英文:
Java dynamically add a button to a panel
问题
我有一个面板和一个按钮1。当我按下按钮时,我想在面板内创建其他按钮(具有特定的高度和宽度)。所以我按一次按钮1,它会在面板内创建一个按钮。我再次按下按钮1,它会在面板内创建另一个按钮,紧挨着另一个按钮或者有一些间隔。有任何帮助吗?我尝试了
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
JButton jButton = new JButton("按钮");
panel1.add(jButton);
validate();
}
英文:
I have a panel, and a button1. When I press the button, I want to create other buttons (of certain height and width) inside the panel. So I press the button1 once, it creates a button in the panel. I press button1 again, it creates another button in the panel right next to the other button or with some space in-between. Any help? I tried
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
JButton jButton = new JButton("Button");
panel1.add(jButton);
validate();
}
答案1
得分: 0
我假设您已经可以访问面板,因为您正试图从方法内部访问它。
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
JButton button = new JButton("按钮");
button.setVisible(true);
panel1.add(button);
panel1.revalidate();
}
要为按钮选择特定位置,这完全取决于JPanel使用的布局类型。
我认为FlowLayout(这是JPanel的默认布局)应该会将按钮放在一行中,直到其父容器水平空间不足以容纳更多按钮为止,然后它将开始下一行。
有关布局的Oracle指南可以在这里找到。
英文:
I presume you already have access to the panel considering you are trying to access it from inside the method.
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
JButton button = new JButton("Button");
button.setVisible(true);
panel1.add(button);
panel1.revalidate();
}
To choose a specific location for your button it all depends on what type of layout the JPanel has.
I believe the FlowLayout (which is the default layout for a JPanel) should place the button next to each other in a row until it's parent container has no more room horizontally according to it's parent, then it will start on the next row.
A guide to layouts by Oracle can be found Here
答案2
得分: 0
这里有一个可以实现你所需功能的 SSCCE 示例代码。
它使用 BoxLayout 来确保按钮在同一行内显示。
它将每个按钮设置为相同的特定大小。
每个相邻按钮之间保持相同的间隔。
它使用 JScrollPane,这样一旦你添加的按钮超出了 JScrollPane
的宽度,你可以滚动查看其他按钮。而不是每次添加按钮时都增加 JPanel
的大小(或创建新行)。
代码后有更多解释。
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.WindowConstants;
public class AdButton implements ActionListener, Runnable {
private static final String ADD = "Add";
private static final String EXIT = "Exit";
private int counter;
private JFrame frame;
private JPanel panel;
@Override // java.awt.event.ActionListener
public void actionPerformed(ActionEvent event) {
String actionCommand = event.getActionCommand();
switch (actionCommand) {
case ADD:
addButton();
break;
case EXIT:
System.exit(0);
default:
JOptionPane.showMessageDialog(frame,
actionCommand,
"Unhandled",
JOptionPane.WARNING_MESSAGE);
}
}
@Override // java.lang.Runnable
public void run() {
createGui();
}
private void addButton() {
panel.add(Box.createHorizontalStrut(10));
JButton button = new JButton(String.valueOf(++counter));
Dimension size = new Dimension(50, 30);
button.setMaximumSize(size);
button.setMinimumSize(size);
button.setPreferredSize(size);
panel.add(button);
panel.revalidate();
}
private JButton createButton(String text, int mnemonic, String tooltip) {
JButton button = new JButton(text);
button.setMnemonic(mnemonic);
button.setToolTipText(tooltip);
button.addActionListener(this);
return button;
}
private JPanel createButtonsPanel() {
JPanel buttonsPanel = new JPanel();
buttonsPanel.add(createButton(ADD, KeyEvent.VK_A, "Add button to panel"));
buttonsPanel.add(createButton(EXIT, KeyEvent.VK_X, "Exit the application"));
return buttonsPanel;
}
private void createGui() {
frame = new JFrame("Add Buttons");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(createPanel(), BorderLayout.CENTER);
frame.add(createButtonsPanel(), BorderLayout.PAGE_END);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JScrollPane createPanel() {
panel = new JPanel();
LayoutManager mgr = new BoxLayout(panel, BoxLayout.LINE_AXIS);
panel.setLayout(mgr);
JScrollPane scrollPane = new JScrollPane(panel,
ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setPreferredSize(new Dimension(500, 100));
return scrollPane;
}
public static void main(String[] args) {
EventQueue.invokeLater(new AdButton());
}
}
当你想要通过点击 JButton
执行操作时,应该使用 ActionListener 而不是 MouseListener
。
在 addButton()
方法中的重要部分是调用 revalidate()
,这会使 JPanel
对其所有组件进行布局并将它们绘制在屏幕上。你需要这样做,因为你已经添加了另一个组件到 JPanel
。
英文:
Here's a SSCCE that does what you desire.
It uses BoxLayout to ensure that the buttons are displayed in a single row.
It sets each button to the same, particular size.
There is the same size gap between each, adjacent button.
It uses a JScrollPane so that once you add more buttons that can fit into the width of the JScrollPane
, you can scroll to see the other buttons. This is instead of increasing the size of the JPanel
(or creating a new row) each time you add a button.
More explanations after the code.
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.WindowConstants;
public class AdButton implements ActionListener, Runnable {
private static final String ADD = "Add";
private static final String EXIT = "Exit";
private int counter;
private JFrame frame;
private JPanel panel;
@Override // java.awt.event.ActionListener
public void actionPerformed(ActionEvent event) {
String actionCommand = event.getActionCommand();
switch (actionCommand) {
case ADD:
addButton();
break;
case EXIT:
System.exit(0);
default:
JOptionPane.showMessageDialog(frame,
actionCommand,
"Unhandled",
JOptionPane.WARNING_MESSAGE);
}
}
@Override // java.lang.Runnable
public void run() {
createGui();
}
private void addButton() {
panel.add(Box.createHorizontalStrut(10));
JButton button = new JButton(String.valueOf(++counter));
Dimension size = new Dimension(50, 30);
button.setMaximumSize(size);
button.setMinimumSize(size);
button.setPreferredSize(size);
panel.add(button);
panel.revalidate();
}
private JButton createButton(String text, int mnemonic, String tooltip) {
JButton button = new JButton(text);
button.setMnemonic(mnemonic);
button.setToolTipText(tooltip);
button.addActionListener(this);
return button;
}
private JPanel createButtonsPanel() {
JPanel buttonsPanel = new JPanel();
buttonsPanel.add(createButton(ADD, KeyEvent.VK_A, "Add button to panel"));
buttonsPanel.add(createButton(EXIT, KeyEvent.VK_X, "Exit the application"));
return buttonsPanel;
}
private void createGui() {
frame = new JFrame("Add Buttons");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(createPanel(), BorderLayout.CENTER);
frame.add(createButtonsPanel(), BorderLayout.PAGE_END);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JScrollPane createPanel() {
panel = new JPanel();
LayoutManager mgr = new BoxLayout(panel, BoxLayout.LINE_AXIS);
panel.setLayout(mgr);
JScrollPane scrollPane = new JScrollPane(panel,
ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setPreferredSize(new Dimension(500, 100));
return scrollPane;
}
public static void main(String[] args) {
EventQueue.invokeLater(new AdButton());
}
}
You should use an ActionListener rather than a MouseListener
when you want an action to be performed as a result of clicking in a JButton
.
The important part in the addButton()
method is the call to revalidate()
which makes the JPanel
layout all its components and draw them on the screen. You need to do this since you have added another component to the JPanel
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论