GridLayout 不通过单击按钮添加组件?
GridLayout do not add a component by button click?
在程序启动时添加 JButton
组件时,稍后尝试通过例如添加一些按钮时它不会工作。单击按钮。
可能只是语法问题。
可能是什么原因?
下面代码中的示例程序以 CardLayout
开头,可以通过单击按钮将其滚动到也有一个按钮的看板,每次单击应添加一个按钮。
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class GridTest {
JFrame f;
BorderLayout bl;
JPanel p;
JPanel start;
JPanel cbb;
JPanel grid;
JPanel board;
CardLayout c;
CardLayout cl;
JTextArea ta2;
JButton sb2;
JButton bb2;
JButton cb;
JButton getButton;
JLabel aCross;
JLabel back;
String controls = "Controls";
String boardS = "Board";
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
new GridTest().startApp();
} catch (ClassNotFoundException ex) {
Logger.getLogger(GridTest.class.getName()).log(Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
Logger.getLogger(GridTest.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(GridTest.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedLookAndFeelException ex) {
Logger.getLogger(GridTest.class.getName()).log(Level.SEVERE, null, ex);
} catch (ParseException ex) {
Logger.getLogger(GridTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
private void startApp() throws ParseException {
c = new CardLayout();
cl = new CardLayout();
p = new JPanel(c);
p.setOpaque(false);
start = new JPanel();
start.setLayout(new BorderLayout());
board = new JPanel();
board.setLayout(new BorderLayout());
grid = new JPanel();
grid.setLayout(new GridLayout(2,3));
cbb = new JPanel();
cbb.setLayout(new FlowLayout());
cb = new JButton("Board");
cb.addActionListener(new BoardListener());
bb2 = new JButton("Back");
bb2.addActionListener(new BackListener());
getButton = new JButton("GetButton");
getButton.addActionListener(new GetButtonListener());
start.add(BorderLayout.NORTH, cb);
start.add(BorderLayout.CENTER, new JLabel("Center"));
start.setBackground(Color.WHITE);
cbb.add(bb2);
cbb.add(getButton);
grid.add(new JButton("Button 1"));
grid.setBackground(Color.BLACK);
board.add(BorderLayout.NORTH, cbb);
board.add(BorderLayout.CENTER, grid);
p.add(start, controls);
p.add(board, boardS);
cl = (CardLayout)(p.getLayout());
cl.first(p);
f = new JFrame();
f.getContentPane().add(p, BorderLayout.CENTER);
f.setSize(450,550);
f.setResizable(false);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private class BackListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent ev) {
cl.first(p);
}
}
private class BoardListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent ev) {
cl.show(p, boardS);
}
}
private class GetButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent ev) {
grid.add(new JButton("Button 2")); //how to get this in the Grid ?
}
}
}
虽然添加了按钮,但是UI没有更新。尝试添加以下行。
grid.revalidate();
因此您的 GetButtonListener 应该如下所示:
private class GetButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent ev) {
grid.add(new JButton("Button 2")); //how to get this in the Grid ?
grid.revalidate();
}
}
在程序启动时添加 JButton
组件时,稍后尝试通过例如添加一些按钮时它不会工作。单击按钮。
可能只是语法问题。
可能是什么原因?
下面代码中的示例程序以 CardLayout
开头,可以通过单击按钮将其滚动到也有一个按钮的看板,每次单击应添加一个按钮。
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class GridTest {
JFrame f;
BorderLayout bl;
JPanel p;
JPanel start;
JPanel cbb;
JPanel grid;
JPanel board;
CardLayout c;
CardLayout cl;
JTextArea ta2;
JButton sb2;
JButton bb2;
JButton cb;
JButton getButton;
JLabel aCross;
JLabel back;
String controls = "Controls";
String boardS = "Board";
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
new GridTest().startApp();
} catch (ClassNotFoundException ex) {
Logger.getLogger(GridTest.class.getName()).log(Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
Logger.getLogger(GridTest.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(GridTest.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedLookAndFeelException ex) {
Logger.getLogger(GridTest.class.getName()).log(Level.SEVERE, null, ex);
} catch (ParseException ex) {
Logger.getLogger(GridTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
private void startApp() throws ParseException {
c = new CardLayout();
cl = new CardLayout();
p = new JPanel(c);
p.setOpaque(false);
start = new JPanel();
start.setLayout(new BorderLayout());
board = new JPanel();
board.setLayout(new BorderLayout());
grid = new JPanel();
grid.setLayout(new GridLayout(2,3));
cbb = new JPanel();
cbb.setLayout(new FlowLayout());
cb = new JButton("Board");
cb.addActionListener(new BoardListener());
bb2 = new JButton("Back");
bb2.addActionListener(new BackListener());
getButton = new JButton("GetButton");
getButton.addActionListener(new GetButtonListener());
start.add(BorderLayout.NORTH, cb);
start.add(BorderLayout.CENTER, new JLabel("Center"));
start.setBackground(Color.WHITE);
cbb.add(bb2);
cbb.add(getButton);
grid.add(new JButton("Button 1"));
grid.setBackground(Color.BLACK);
board.add(BorderLayout.NORTH, cbb);
board.add(BorderLayout.CENTER, grid);
p.add(start, controls);
p.add(board, boardS);
cl = (CardLayout)(p.getLayout());
cl.first(p);
f = new JFrame();
f.getContentPane().add(p, BorderLayout.CENTER);
f.setSize(450,550);
f.setResizable(false);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private class BackListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent ev) {
cl.first(p);
}
}
private class BoardListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent ev) {
cl.show(p, boardS);
}
}
private class GetButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent ev) {
grid.add(new JButton("Button 2")); //how to get this in the Grid ?
}
}
}
虽然添加了按钮,但是UI没有更新。尝试添加以下行。
grid.revalidate();
因此您的 GetButtonListener 应该如下所示:
private class GetButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent ev) {
grid.add(new JButton("Button 2")); //how to get this in the Grid ?
grid.revalidate();
}
}