我正在尝试做一个JPanel,每次点击一个按钮就可以改变它的大小。我的方法是创建两个不同大小的面板。点击后,一个会变得可见,另一个变得不可见。到目前为止,我已经设法使第一个看不见,但我被困在那里。我的方法有什么好处吗?我错过了什么?代码......
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GrowAndShrinkSquareGUI {
JFrame frame;
SquareDrawPanel greenPanel;
SquareDrawPanel greenPanel2;
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);
greenPanel2.setVisible(true);
}}
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);
drawPanel(greenPanel2);
growAndShrinkListener button = new growAndShrinkListener();
//addButton(CreateJButton());
}
private JPanel createRectPanel(int x, int y) {
greenPanel = new SquareDrawPanel(x, y, 100, 100);
return greenPanel;
}
private JPanel createRectPanel2(int x, int y) {
greenPanel2 = new SquareDrawPanel(x, y, 200, 200);
return greenPanel2;
}
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 :(得分:1)
public class CustomFrame extends JFrame implements ActionListener
{
//Creating an object of this class shows a frame which toggles
//between these two sizes on a button click
private static final Dimension FIRST_SIZE = new Dimension(200, 200);
private static final Dimension SECOND_SIZE = new Dimension(400, 400);
public CustomFrame()
{
//Add a button to the frame and register an action listener
//to the current object
JButton button = new JButton("Change size");
button.addActionListener(this);
getContentPane().add(button);
//Make the frame visible
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent ae)
{
//This gets executed when the button is clicked
//
//We're setting the size to a new value;
//first, we fetch the current size and
//check if it's equal to the first size
setSize(getSize().equals(FIRST_SIZE)
//If so, set the frame to the second size
? SECOND_SIZE
//In all other cases, make the frame's
//size the value of FIRST_SIZE
: FIRST_SIZE);
}
}