我正在做一个项目,在那里我需要点击一个按钮来制作另一个按钮。最终,我想对新按钮的位置有更多的控制,并且能够多次创建新的按钮,但是现在.我只想让一个JButton创建另一个JButton。
使用下面的代码,我的目标是让White B1创建一个红色B3按钮。我还希望蓝色B2按钮创建一个绿色的B4按钮。
最后,我还希望B3和B4 (按钮生成的按钮)能够让用户点击这些按钮并使其消失。
这两个按钮似乎都不起什么作用,我也不知道为什么。我有三个类文件。知道我哪里可能出问题了吗?
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);
}
}发布于 2019-07-11 01:47:58
为了在单击按钮B3后显示按钮B1,您需要添加.
revalidate();
repaint();排队后..。
add(button3, gc);在文件DetailsPanel.java中。
类似于按钮B2。
发布于 2019-07-10 00:39:31
添加按钮后,必须触发布局管理器的重新绘制。
之后
add(button3, gc);添加
invalidate();
validate();有关详细信息,请参阅Swing文档和失效()和validate()的javadoc。
https://stackoverflow.com/questions/56961921
复制相似问题