javax.swing.JComponent.revalidate()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(9.6k)|赞(0)|评价(0)|浏览(149)

本文整理了Java中javax.swing.JComponent.revalidate()方法的一些代码示例,展示了JComponent.revalidate()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JComponent.revalidate()方法的具体详情如下:
包路径:javax.swing.JComponent
类名称:JComponent
方法名:revalidate

JComponent.revalidate介绍

暂无

代码示例

代码示例来源:origin: stackoverflow.com

this.panel = new JPanel();
this.panel.setLayout(new FlowLayout());
add(panel, BorderLayout.CENTER);
JButton button = new JButton("CLICK HERE");
add(button, BorderLayout.SOUTH);
button.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 500);
setVisible(true);
this.panel.add(new JButton("Button"));
this.panel.revalidate();
validate();

代码示例来源:origin: stackoverflow.com

final JScrollPane scroll = new JScrollPane(panel);
scroll.setPreferredSize(new Dimension(80,100));
gui.add(scroll, BorderLayout.CENTER);
JButton addLabel = new JButton("Add Label");
gui.add(addLabel, BorderLayout.NORTH);
ActionListener listener = new ActionListener() {
  int counter = 0;
  public void actionPerformed(ActionEvent ae) {
    panel.add(new JLabel("Label " + ++counter));
    panel.revalidate();
    int height = (int)panel.getPreferredSize().getHeight();
    scroll.getVerticalScrollBar().setValue(height);

代码示例来源:origin: stackoverflow.com

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class Main {
  public static void main(String[] args) {
   final JComboBox<String> myCombo = new JComboBox<String>(new String[]{"Foo", "Bar"});
   final JPanel mainPanel = new JPanel();
   mainPanel.setPreferredSize(new Dimension(250, 100));
   mainPanel.add(new JButton(new AbstractAction("Toggle Combo") {

     @Override
     public void actionPerformed(ActionEvent arg0) {
      myCombo.setVisible(!myCombo.isVisible());
      mainPanel.revalidate();
      mainPanel.repaint();
     }
   }));
   mainPanel.add(myCombo);

   JOptionPane.showMessageDialog(null, mainPanel);
  }

}

代码示例来源:origin: stackoverflow.com

parent.revalidate();
JFrame frame = new JFrame("play with validation");
frame.add(parent);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);

代码示例来源:origin: stackoverflow.com

for (int col = 0; col < GRID_COLS; col++) {
    panelGrid[row][col] = new JPanel(new GridBagLayout());
    backingPanel.add(panelGrid[row][col]);
redLabel.setBackground(Color.red.brighter().brighter());
redLabel.setPreferredSize(LABEL_SIZE);
panelGrid[4][3].add(redLabel);
panelGrid[1][1].add(blueLabel);
    clickedPanel.revalidate();
    clickedPanel.repaint();
    dragLabel.setLocation(x, y);
    add(dragLabel, JLayeredPane.DRAG_LAYER);
    repaint();
    clickedPanel.add(dragLabel);
    clickedPanel.revalidate();
  } else {
    int r = -1;
      clickedPanel.add(dragLabel);
      clickedPanel.revalidate();
    } else {
      droppedPanel.add(dragLabel);
      droppedPanel.revalidate();

代码示例来源:origin: stackoverflow.com

JScrollPane scroll = new JScrollPane(panel);
scroll.setPreferredSize(new Dimension(80,100));
gui.add(scroll, BorderLayout.CENTER);
JButton addLabel = new JButton("Add Label");
gui.add(addLabel, BorderLayout.NORTH);
ActionListener listener = new ActionListener() {
  int counter = 0;
  public void actionPerformed(ActionEvent ae) {
    panel.add(new JLabel("Label " + ++counter));
    panel.revalidate();
    int height = (int)panel.getPreferredSize().getHeight();
    Rectangle rect = new Rectangle(0,height,10,10);

代码示例来源:origin: stackoverflow.com

JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
    JLabel _lbl = new JLabel("Label");//make label and assign text in 1 line
    panel.add(_lbl);//add label we made
    panel.revalidate();
    panel.repaint();
    frame.pack();//so our frame resizes to compensate for new components
frame.getContentPane().add(panel, BorderLayout.CENTER);
frame.getContentPane().add(button, BorderLayout.SOUTH);

代码示例来源:origin: stackoverflow.com

if (useSlideButton) {
  final JPanel statusPanel = new JPanel();
  basePanel.add(statusPanel, BorderLayout.SOUTH);
  statusPanel.add(new JButton("Slide Left") {
    private static final long serialVersionUID = 9204819004142223529L;
  statusPanel.add(new JButton("Slide Right") {
  basePanel.add(componentNew);
  basePanel.revalidate();
  if (useLoop) {
    final int max = (slideType == LEFT) || (slideType == RIGHT) ? w : h;

代码示例来源:origin: stackoverflow.com

JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new BorderLayout());
final JPanel buttons = new JPanel();
panel.add(pane);
frame.setContentPane(panel);
frame.setSize(300, 200);
frame.setVisible(true);
SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
    for (int i = 0; i < 10; i++) {
      Thread.sleep(800);
      buttons.add(new JButton("Hello " + i));
      buttons.revalidate();

代码示例来源:origin: stackoverflow.com

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new JScrollPane(new TestPane()));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
  double delta = 0.05f * e.getPreciseWheelRotation();
  scale += delta;
  revalidate();
  repaint();

代码示例来源:origin: stackoverflow.com

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new JScrollPane(new TestPane()));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
  double delta = 0.05f * e.getPreciseWheelRotation();
  scale += delta;
  revalidate();
  repaint();

代码示例来源:origin: stackoverflow.com

b.setPreferredSize(new Dimension(600, 10));
panel = new JPanel(new GridLayout(0, 1));
panel.add(b);
f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(panel, "Center");
f.add(getCheckBoxPanel(), "South");
f.setLocation(200, 200);
f.pack();
f.setVisible(true);
  panel.revalidate();
  panel.repaint();
  f.pack();

代码示例来源:origin: stackoverflow.com

DragPanel p1 = new DragPanel();
p1.setBorder(BorderFactory.createLineBorder(Color.BLACK));
p1.add(new JLabel(UIManager.getIcon("OptionPane.warningIcon")));
p1.add(new JLabel(UIManager.getIcon("OptionPane.errorIcon")));
p1.add(new JLabel("Label1"));
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);
f.setVisible(true);
 target.revalidate();
 return true;
} catch(UnsupportedFlavorException ufe) {
if(action == TransferHandler.MOVE) {
 src.remove(src.draggingLabel);
 src.revalidate();
 src.repaint();
window.setVisible(false);

代码示例来源:origin: stackoverflow.com

main.add(buttons, BorderLayout.CENTER);
  buttons.add( new JRadioButton("button " + i) );
main.add(button, BorderLayout.SOUTH);
button.addActionListener( new ActionListener()
    buttons.add( new JRadioButton("button R Us" + i++) );
    buttons.revalidate();
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(main);
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);

代码示例来源:origin: stackoverflow.com

JPanel container = (JPanel) src.getParent();
   container.remove(src);
   container.revalidate();
   container.repaint();
  btnPanel.add(removeBtn);
borderPanel.add(btnPanel, BorderLayout.NORTH);
JScrollPane scrollpane = new JScrollPane(borderPanel,
   JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
scrollpane.setPreferredSize(new Dimension(400, 800));
JFrame frame = new JFrame("Foo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(scrollpane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);

代码示例来源:origin: stackoverflow.com

import javax.swing.*;

public class ButtonsInScrollPane{

  public static void main(String[] args){
    JFrame frame = new JFrame();
    JPanel p = new JPanel();
    p.setLayout(new BoxLayout(p, BoxLayout.PAGE_AXIS));
    p.add(getJButton(p));
    JScrollPane scroll = new JScrollPane(p);
    frame.setContentPane(scroll);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200, 200);
    frame.setVisible(true);
  }

  static public JButton getJButton(JPanel p){
    JButton b = new JButton("more");
    b.addActionListener(evt->{
      p.add(getJButton(p));
      p.revalidate();
      p.repaint();
      });
    return b;
  }
}

代码示例来源:origin: stackoverflow.com

centerPanel.add(new JButton("X"));
   centerPanel.revalidate();
   centerPanel.repaint();  
   SwingUtilities.getWindowAncestor(centerPanel).pack();
btnPanel.add(addBtn);
mainPanel.add(centerPanel, BorderLayout.CENTER);
mainPanel.add(btnPanel, BorderLayout.PAGE_END);
JFrame frame = new JFrame("GridLayoutEg");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);

代码示例来源:origin: stackoverflow.com

constraints.gridy = i;
  panel.add(field, constraints);
    if (panel.getComponentCount() >= 1) {
      panel.remove(panel.getComponentCount() - 1);
      scrollPane.revalidate();
      scrollPane.repaint();
JFrame frame = new JFrame("Swing Test");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(640, 480);
frame.setLocation(200, 200);
frame.getContentPane().add(scrollPane);
frame.getContentPane().add(removeButton, BorderLayout.SOUTH);
frame.setVisible(true);

代码示例来源:origin: stackoverflow.com

add(b1, BorderLayout.CENTER);
      repaint();
      revalidate();
      repaint();
      revalidate();
      isthere = !isthere;
JFrame frame = new JFrame();
frame.add(new Animation());
frame.setVisible(true);
frame.setSize(1000, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

代码示例来源:origin: stackoverflow.com

import java.awt.event.ActionEvent;
import javax.swing.*;

public class FrameTestBase extends JFrame {

  public static void main(String args[]) {
    FrameTestBase t = new FrameTestBase();

    final JPanel p = new JPanel();

    final JButton button = new JButton();

    button.setAction(new AbstractAction("Remove me!") {
      @Override
      public void actionPerformed(ActionEvent e) {
        p.remove(button);
        p.revalidate();
        p.repaint();
      }
    });

    p.add(button);
    t.setContentPane(p);

    t.setDefaultCloseOperation(EXIT_ON_CLOSE);
    t.setSize(400, 400);
    t.setVisible(true);
  }
}

相关文章

JComponent类方法