我正在开发一个项目,我需要单击一个按钮来创建另一个按钮。最终,我希望对新按钮的位置有更多的控制,并能够多次创建新按钮,但现在......我希望只让一个 JButton 创建另一个 JButton。
使用下面的代码,我的目标是让白色 B1 创建红色 B3 按钮。我还想要蓝色 B2 按钮创建绿色 B4 按钮。
最终,我还希望 B3 和 B4(从按钮生成的按钮)让用户能够单击它们并使其消失。
这两个按钮似乎都不起作用,我不明白为什么。我有3个类文件。知道我哪里可能出错吗?
Window.Java
package gui;
import javax.swing.JFrame;
public class Window {
public static void main(String[] args) {
//frame creation
JFrame frame = new MainFrame("Button Create Button Test");
//frame size
frame.setSize(800, 800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
DetailsPanel.Java
package gui;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class DetailsPanel extends JPanel {
public DetailsPanel() {
Dimension size = getPreferredSize();
size.width = 400;
setPreferredSize(size);
///Buttons
JButton button1 = new JButton("B1");
button1.setPreferredSize(new Dimension (72, 73));
button1.setBackground(Color.WHITE);
button1.setBorderPainted(true);
JButton button2 = new JButton("B2");
button2.setPreferredSize(new Dimension (72, 73));
button2.setBackground(Color.BLUE);
button2.setBorderPainted(true);
setLayout (new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
/// Layout ///
/// Row 1 ///
gc.anchor = GridBagConstraints.NORTH;
gc.weightx = 0.5;
gc.weighty = 0.5;
gc.gridx = 1;
gc.gridy = 1;
add(button1, gc);
gc.gridx = 1;
gc.gridy = 2;
add(button2, gc);
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JButton button3 = new JButton("B3");
button3.setPreferredSize(new Dimension (72, 73));
button3.setBackground(Color.RED);
button3.setBorderPainted(true);
gc.gridx = 1;
gc.gridy = 3;
add(button3, gc);
}
});
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JButton button4 = new JButton("B3");
button4.setPreferredSize(new Dimension (72, 73));
button4.setBackground(Color.GREEN);
button4.setBorderPainted(true);
gc.gridx = 1;
gc.gridy = 4;
add(button4, gc);
}
});
}
}
MainFrame.Java
package gui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class MainFrame extends JFrame {
private DetailsPanel detailsPanel;
public MainFrame(String title) {
super(title);
// set layout manager
setLayout (new BorderLayout());
// Create Swing Component
detailsPanel = new DetailsPanel();
detailsPanel.setBackground(Color.BLACK);
// Add swing components to content pane
Container container = getContentPane();
container.add(detailsPanel, BorderLayout.WEST);
}
}
最佳答案
为了在单击按钮 B1 后显示按钮 B3,您需要添加...
revalidate();
repaint();
行后...
add(button3, gc);
在文件DetailsPanel.java
中。
按钮 B2 也类似。
关于java - 通过单击 JButton 创建 JButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56961921/