我有两个JPanel的问题。两者都用绿色矩形表示,一个比另一个大。我一直试图从一个面板交换到另一个面板,这意味着,当您单击按钮时,一个面板将替换另一个面板。但是我无法找到第二个出现的解决方案。我得到的最多是第一次隐藏。我该如何关注它?我没有想法。
public class GrowAndShrinkSquareGUI {
JFrame frame;
SquareDrawPanel greenPanel;
public class SquareDrawPanel extends JPanel {
int locationX;
int locationY;
int width;
int height;
SquareDrawPanel(int x, int y, int w, int h) {
locationX = x;
locationY = y;
width = w;
height = h;
}
public void paintComponent(Graphics g) {
g.setColor(Color.green);
g.fillRect(locationX, locationY, width, height);
}
}
public class growAndShrinkListener implements ActionListener {
JButton button;
public growAndShrinkListener() {
JButton button = new JButton("Click me to grow the Square");
frame.add(button, BorderLayout.NORTH);
button.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.print("clicked");
greenPanel.setVisible(false);
}
}
public static void main(String[] args) {
GrowAndShrinkSquareGUI test = new GrowAndShrinkSquareGUI();
test.go();
}
public void go() {
frame = new JFrame();
frame.setSize(500, 500);
frame.setVisible(true);
frame.setLayout(new BorderLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
drawPanel(greenPanel);
growAndShrinkListener button = new growAndShrinkListener();
//addButton(CreateJButton());
}
private JPanel createRectPanel(int x, int y) {
greenPanel = new SquareDrawPanel(x, y, 100, 100);
return greenPanel;
}
private void drawPanel(JPanel panel) {
panel = createRectPanel(setLocationX(), setLocationY());
frame.add(panel, BorderLayout.CENTER); // DoesNot run properly
}
private int setLocationX() {
int centeredX = frame.getWidth() / 2 - 50;
return centeredX;
}
private int setLocationY() {
int centeredY = frame.getHeight() / 2 - 75;
return centeredY;
}
}
答案 0 :(得分:0)
对于此特定任务,您应使用CardLayout
,这样您就可以在“卡片”之间快速切换。卡片基本上是JComponent
与名称相关联的:
public class GrowAndShrinkSquareGUI {
CardLayout layout;
JPanel cardPanel; // Holds the cards
public class SquareDrawPanel extends JPanel {
int locationX;
int locationY;
int width;
int height;
SquareDrawPanel(int x, int y, int w, int h) {
locationX = x;
locationY = y;
width = w;
height = h;
}
public void paintComponent(Graphics g) {
g.setColor(Color.green);
g.fillRect(locationX, locationY, width, height);
}
}
public static void main(String[] args) {
GrowAndShrinkSquareGUI test = new GrowAndShrinkSquareGUI();
test.go();
}
public void go() {
JFrame frame = new JFrame();
frame.setSize(500, 500);
frame.setVisible(true);
frame.setLayout(new BorderLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Set up CardLayout
layout = new CardLayout();
// Set up the first panel
JPanel firstPanel = new JPanel(new BorderLayout());
JButton switchButton = new JButton("Switch to green panel!");
switchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
layout.show(cardPanel, "green");
}
}
firstPanel.add(switchButton, BorderLayout.NORTH);
layout.add(firstPanel, "first");
// Set up the second panel
JPanel secondPanel = new SquareDrawPanel(5, 5, 50, 50);
layout.add(secondPanel, "green");
// Set up the main panel
cardPanel = new JPanel(layout);
frame.add(cardPanel);
}
private int setLocationX() {
int centeredX = frame.getWidth() / 2 - 50;
return centeredX;
}
private int setLocationY() {
int centeredY = frame.getHeight() / 2 - 75;
return centeredY;
}
}