java - java - 如何在Java Swing中使用开头未加载的jbutton?
问题描述
我正在学习 java swing,我有很多按钮,但是当我在组合框上选择某些东西时,我需要在红色区域中放置一些新按钮,问题是我不知道如何使用这些按钮一开始就创建了。
当我在组合框中选择时,这里加载了一些按钮,我需要对它们进行一些操作:
谢谢
解决方案
你的问题留下了很多解释。
因此,例如,如果值是固定的,我会首先将它们分成单独的面板,隔离它们的功能和工作流程,然后根据选择的内容简单地交换它们。
import java.awt.CardLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
JComboBox<String> comboBox = new JComboBox<>();
DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>();
model.addElement("Option A");
model.addElement("Option B");
comboBox.setModel(model);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(8, 8, 8, 8);
add(comboBox, gbc);
CardLayout cardLayout = new CardLayout();
JPanel buttonOptions = new JPanel(cardLayout);
buttonOptions.add(new OptionAPane(), "optionA");
buttonOptions.add(new OptionBPane(), "optionB");
gbc.gridy = 1;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
add(buttonOptions, gbc);
comboBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
switch (comboBox.getSelectedIndex()) {
case 0:
cardLayout.show(buttonOptions, "optionA");
break;
case 1:
cardLayout.show(buttonOptions, "optionB");
break;
}
}
});
}
}
public class OptionAPane extends JPanel {
public OptionAPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = gbc.REMAINDER;
gbc.insets = new Insets(8, 8, 8, 8);
JButton btnThis = new JButton("This");
btnThis.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Do this");
}
});
JButton btnThat = new JButton("That");
btnThis.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Do that");
}
});
add(new JLabel("A Options"), gbc);
add(btnThis, gbc);
add(btnThat, gbc);
}
}
public class OptionBPane extends JPanel {
public OptionBPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = gbc.REMAINDER;
gbc.insets = new Insets(8, 8, 8, 8);
JButton btnOther = new JButton("Other");
btnOther.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Do other");
}
});
JButton btnSomethingElse = new JButton("Something else");
btnOther.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Do something else");
}
});
add(new JLabel("B Options"), gbc);
add(btnOther, gbc);
add(btnSomethingElse, gbc);
}
}
}
但是我怎么知道什么时候点击了一个按钮。
这是将使用观察者模式的地方。您将根据每个面板的需要定义一个或多个侦听器,当单击面板上的按钮时,该侦听器将触发一个事件,您可以使用它
另一方面,如果您需要一些更动态的东西(即每个选项可以根据其他配置有不同的操作),您也许可以使用Action
API
例如...
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class ActionGroup {
private String description;
private List<Action> actions;
public ActionGroup(String description) {
this.description = description;
this.actions = new ArrayList<>(25);
}
public ActionGroup(String description, List<Action> actions) {
this.description = description;
this.actions = actions;
}
public String getDescription() {
return description;
}
public void add(Action action) {
actions.add(action);
}
public void remove(Action action) {
actions.remove(action);
}
public List<Action> getActions() {
return Collections.unmodifiableList(actions);
}
}
public class SimpleAction extends AbstractAction {
// You have a number of choices when using something like Action,
// You can create a custom Action based on your needs and pass
// in the information it needs to do it's job OR, you can use
// a observer pattern to get notified when the ActionListener
// is triggered
public SimpleAction(String name) {
putValue(NAME, name);
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(getValue(NAME) + " was triggered");
}
}
public class ActionGroupCellRenderer extends DefaultListCellRenderer {
@Override
public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
if (value instanceof ActionGroup) {
value = ((ActionGroup)value).getDescription();
}
return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
}
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
ActionGroup optionA = new ActionGroup("Option A");
optionA.add(new SimpleAction("Cat"));
optionA.add(new SimpleAction("Doggy"));
optionA.add(new SimpleAction("Bunny"));
optionA.add(new SimpleAction("Rat"));
optionA.add(new SimpleAction("Cow"));
ActionGroup optionB = new ActionGroup("Option B");
optionB.add(new SimpleAction("Banana"));
optionB.add(new SimpleAction("Apple"));
optionB.add(new SimpleAction("Pear"));
optionB.add(new SimpleAction("Orange"));
optionB.add(new SimpleAction("Lemon"));
JComboBox<ActionGroup> comboBox = new JComboBox<>();
comboBox.setRenderer(new ActionGroupCellRenderer());
DefaultComboBoxModel<ActionGroup> model = new DefaultComboBoxModel<>();
model.addElement(optionA);
model.addElement(optionB);
comboBox.setModel(model);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(8, 8, 8, 8);
add(comboBox, gbc);
gbc.gridy = 1;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
ActionGroupPane buttonOptions = new ActionGroupPane();
buttonOptions.setActionGroup(optionA);
add(buttonOptions, gbc);
comboBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Object item = comboBox.getSelectedItem();
if (item instanceof ActionGroup) {
System.out.println(item);
buttonOptions.setActionGroup((ActionGroup)item);
}
}
});
}
}
public class ActionGroupPane extends JPanel {
public ActionGroupPane() {
setLayout(new GridBagLayout());
}
public void setActionGroup(ActionGroup actionGroup) {
removeAll();
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = gbc.REMAINDER;
gbc.insets = new Insets(8, 8, 8, 8);
add(new JLabel(actionGroup.getDescription()), gbc);
for (Action action : actionGroup.getActions()) {
add(new JButton(action), gbc);
}
revalidate();
repaint();
}
}
}
Action
非常强大,我建议花一些时间看看如何使用操作
推荐阅读
- c# - Excel到datagridview:不向网格c#Web应用程序显示编号的单元格
- spring - 由 Hibernate 和通过脚本手动生成的主键的约束冲突
- php - 用php获取两个参数之间的所有子字符串
- javascript - Jquery 浏览 div
- regex - 垃圾邮件过滤器的正则表达式
- javascript - 使用 Javascript + Firebase 动态构建的图片库
- sockets - Docker在主机和容器之间建立tcp通信
- php - Api 平台:渴望加载子资源
- r - group_by 在因子列上的性能
- c# - 在 Visual Studio 2017 中调试时禁用内联自动