- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我编写了一些代码来动态创建 JPanel。我现在需要一种方法来捕获用户在这些 JPanel 中创建的数据。我目前有一个“创建”按钮。触发后,我希望此按钮使用每个面板中的 3 种不同数据类型创建一个对象:名称(字符串)、计数(整数)和 boolean 值。
我对编程很陌生,但我面临的主要障碍是如何访问未像这些面板那样命名的动态对象?我弄清楚了如何添加和删除它们,但我无法弄清楚从 Pane 中的字段捕获数据的方法。
谢谢,代码如下:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
public class MainSnakeGUI {
public static void main(String[] arg) {
new MainSnakeGUI();
}
public MainSnakeGUI() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
//Create the frame
JFrame frame = new JFrame("Snake Channels");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new SnakePane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class SnakePane extends JPanel {
public SnakePane() {
//Create buttons and labels
JButton btnAdd = new JButton("Add New Snake");
JButton btnRemove = new JButton("Delete Snake");
JLabel headerLabel = new JLabel("Create a Split Snake");
JButton btnCreate = new JButton("Create");
setLayout(new BorderLayout());
//Create JPanels
JPanel buttons = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel header = new JPanel(new FlowLayout(FlowLayout.CENTER));
JPanel footer = new JPanel(new FlowLayout(FlowLayout.CENTER));
//add items to JPanels
buttons.add(btnAdd);
buttons.add(btnRemove);
header.add(headerLabel);
footer.add(btnCreate);
//add JPanels to window
add(header, BorderLayout.NORTH); //sets header at the top of window
header.setBorder(new CompoundBorder(new LineBorder(Color.BLACK), new EmptyBorder(10, 10, 10, 10)));
add(buttons, BorderLayout.BEFORE_LINE_BEGINS); //sets the button to the left of the display pane
buttons.setBorder(new CompoundBorder(new LineBorder(Color.BLACK), new EmptyBorder(10, 10, 10, 10)));
add(footer, BorderLayout.AFTER_LAST_LINE); //sets buttons on the bottom of window
footer.setBorder(new CompoundBorder(new LineBorder(Color.BLACK), new EmptyBorder(10, 10, 10, 10)));
//add panel to hold new frames
final JPanel content = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weighty = 1;
content.add(new JPanel(), gbc);
//add scroll pane?
add(new JScrollPane(content));
//add listener for button
btnAdd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
SplitSnakeCreationPane pane = new SplitSnakeCreationPane(); //create a new SnakeCreationPane
int insertAt = Math.max(0, content.getComponentCount() - 1); //insert it at the end of the list
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1;
content.add(pane, gbc, insertAt); //add the pane to the window
content.revalidate();
content.repaint();
}
});
btnRemove.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int paneCount = content.getComponentCount()-1; //get count of the pane
content.remove(content.getComponent(paneCount-1)); //remove the pane at paneCount
content.revalidate(); //update window with changes
content.repaint();
SplitSnakeCreationPane.snakeCount--; //update count for snakes
}
});
btnCreate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int paneCount = content.getComponentCount()-1; //get count of the pane
for (int i=0; i<paneCount;){
}
}
});
}
//set size of window
@Override
public Dimension getPreferredSize() {
return new Dimension(1200, 400);
}
}
public static class SplitSnakeCreationPane extends JPanel {
//Count of how many snakes created
private static int snakeCount = 1;
public SplitSnakeCreationPane() {
//creates Snake Panes
setLayout(new GridLayout(2, 4, 10, 10));
add(new JLabel("Split Snake " + (snakeCount++) + "."));
add(new JLabel("Snake Name"));
add(new JLabel("Channel Count"));
add(new JLabel(""));
add(new JLabel(""));
JTextField snakeName = new JTextField(30);
JTextField channelCount = new JTextField("0",3);
JCheckBox artistSupplied = new JCheckBox("Artist Supplied?");
add(snakeName); //Snake Name field
add(channelCount); //Channel Count field
add(artistSupplied); //Artist supplied checkbox
setBorder(new CompoundBorder(new LineBorder(Color.BLACK), new EmptyBorder(10, 10, 10, 10)));
}
}
}
最佳答案
您不需要将这些字段作为局部变量放在构造函数中,而是需要在 SplitSnakeCreationPane 中使用类变量,然后添加一个公共(public) get 方法来从这些字段中获取值。只要你有这些 get 方法,你就可以在任何需要的地方使用它来获取字段值。
查看修改后的类如下:
public static class SplitSnakeCreationPane extends JPanel {
//Count of how many snakes created
private static int snakeCount = 1;
private JTextField snakeName = null;
private JTextField channelCount = null;
private JCheckBox artistSupplied = null;
public SplitSnakeCreationPane() {
//creates Snake Panes
setLayout(new GridLayout(2, 4, 10, 10));
add(new JLabel("Split Snake " + (snakeCount++) + "."));
add(new JLabel("Snake Name"));
add(new JLabel("Channel Count"));
add(new JLabel(""));
add(new JLabel(""));
snakeName = new JTextField(30);
channelCount = new JTextField("0",3);
artistSupplied = new JCheckBox("Artist Supplied?");
add(snakeName); //Snake Name field
add(channelCount); //Channel Count field
add(artistSupplied); //Artist supplied checkbox
setBorder(new CompoundBorder(new LineBorder(Color.BLACK), new EmptyBorder(10, 10, 10, 10)));
}
public String getSnakeName() {
return snakeName.getText();
}
public String getChannelCount() {
return channelCount.getText();
}
public boolean getArtistSupplied() {
return artistSupplied.isSelected();
}
}
这是完整的代码,复制粘贴并运行:结果将打印在控制台上。希望您能了解如何获取这些值
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
public class MainSnakeGUI {
public static void main(String[] arg) {
new MainSnakeGUI();
}
public MainSnakeGUI() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
//Create the frame
JFrame frame = new JFrame("Snake Channels");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new SnakePane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class SnakePane extends JPanel {
public SnakePane() {
//Create buttons and labels
JButton btnAdd = new JButton("Add New Snake");
JButton btnRemove = new JButton("Delete Snake");
JButton show = new JButton("show value");
JLabel headerLabel = new JLabel("Create a Split Snake");
JButton btnCreate = new JButton("Create");
setLayout(new BorderLayout());
//Create JPanels
JPanel buttons = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel header = new JPanel(new FlowLayout(FlowLayout.CENTER));
JPanel footer = new JPanel(new FlowLayout(FlowLayout.CENTER));
//add items to JPanels
buttons.add(btnAdd);
buttons.add(btnRemove);
buttons.add(show);
header.add(headerLabel);
footer.add(btnCreate);
//add JPanels to window
add(header, BorderLayout.NORTH); //sets header at the top of window
header.setBorder(new CompoundBorder(new LineBorder(Color.BLACK), new EmptyBorder(10, 10, 10, 10)));
add(buttons, BorderLayout.BEFORE_LINE_BEGINS); //sets the button to the left of the display pane
buttons.setBorder(new CompoundBorder(new LineBorder(Color.BLACK), new EmptyBorder(10, 10, 10, 10)));
add(footer, BorderLayout.AFTER_LAST_LINE); //sets buttons on the bottom of window
footer.setBorder(new CompoundBorder(new LineBorder(Color.BLACK), new EmptyBorder(10, 10, 10, 10)));
//add panel to hold new frames
final JPanel content = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weighty = 1;
content.add(new JPanel(), gbc);
//add scroll pane?
add(new JScrollPane(content));
//add listener for button
btnAdd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
SplitSnakeCreationPane pane = new SplitSnakeCreationPane(); //create a new SnakeCreationPane
int insertAt = Math.max(0, content.getComponentCount() - 1); //insert it at the end of the list
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1;
content.add(pane, gbc, insertAt); //add the pane to the window
System.out.println(insertAt);
content.revalidate();
content.repaint();
}
});
btnRemove.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int paneCount = content.getComponentCount()-1; //get count of the pane
content.remove(content.getComponent(paneCount-1)); //remove the pane at paneCount
content.revalidate(); //update window with changes
content.repaint();
SplitSnakeCreationPane.snakeCount--; //update count for snakes
}
});
show.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for(int n=0; n<content.getComponentCount(); n++){
if(content.getComponent(n) instanceof SplitSnakeCreationPane){
SplitSnakeCreationPane com = (SplitSnakeCreationPane) content.getComponent(n);
System.out.println("Sname Name is : " + com.getSnakeName());
System.out.println("ChannelCount is : " + com.getChannelCount());
System.out.println("ArtistSupplied is : " + com.getArtistSupplied());
}
}
}
});
btnCreate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int paneCount = content.getComponentCount()-1; //get count of the pane
for (int i=0; i<paneCount;){
}
}
});
}
//set size of window
@Override
public Dimension getPreferredSize() {
return new Dimension(1200, 400);
}
}
public static class SplitSnakeCreationPane extends JPanel {
//Count of how many snakes created
private static int snakeCount = 1;
private JTextField snakeName = null;
private JTextField channelCount = null;
private JCheckBox artistSupplied = null;
public SplitSnakeCreationPane() {
//creates Snake Panes
setLayout(new GridLayout(2, 4, 10, 10));
add(new JLabel("Split Snake " + (snakeCount++) + "."));
add(new JLabel("Snake Name"));
add(new JLabel("Channel Count"));
add(new JLabel(""));
add(new JLabel(""));
snakeName = new JTextField(30);
channelCount = new JTextField("0",3);
artistSupplied = new JCheckBox("Artist Supplied?");
add(snakeName); //Snake Name field
add(channelCount); //Channel Count field
add(artistSupplied); //Artist supplied checkbox
setBorder(new CompoundBorder(new LineBorder(Color.BLACK), new EmptyBorder(10, 10, 10, 10)));
}
public String getSnakeName() {
return snakeName.getText();
}
public String getChannelCount() {
return channelCount.getText();
}
public boolean getArtistSupplied() {
return artistSupplied.isSelected();
}
}
}
关于java - 从动态 JPanel 创建对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31035348/
我的一位教授给了我们一些考试练习题,其中一个问题类似于下面(伪代码): a.setColor(blue); b.setColor(red); a = b; b.setColor(purple); b
我似乎经常使用这个测试 if( object && object !== "null" && object !== "undefined" ){ doSomething(); } 在对象上,我
C# Object/object 是值类型还是引用类型? 我检查过它们可以保留引用,但是这个引用不能用于更改对象。 using System; class MyClass { public s
我在通过 AJAX 发送 json 时遇到问题。 var data = [{"name": "Will", "surname": "Smith", "age": "40"},{"name": "Wil
当我尝试访问我的 View 中的对象 {{result}} 时(我从 Express js 服务器发送该对象),它只显示 [object][object]有谁知道如何获取 JSON 格式的值吗? 这是
我有不同类型的数据(可能是字符串、整数......)。这是一个简单的例子: public static void main(String[] args) { before("one"); }
嗨,我是 json 和 javascript 的新手。 我在这个网站找到了使用json数据作为表格的方法。 我很好奇为什么当我尝试使用 json 数据作为表时,我得到 [Object,Object]
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我听别人说 null == object 比 object == null check 例如: void m1(Object obj ) { if(null == obj) // Is thi
Match 对象 提供了对正则表达式匹配的只读属性的访问。 说明 Match 对象只能通过 RegExp 对象的 Execute 方法来创建,该方法实际上返回了 Match 对象的集合。所有的
Class 对象 使用 Class 语句创建的对象。提供了对类的各种事件的访问。 说明 不允许显式地将一个变量声明为 Class 类型。在 VBScript 的上下文中,“类对象”一词指的是用
Folder 对象 提供对文件夹所有属性的访问。 说明 以下代码举例说明如何获得 Folder 对象并查看它的属性: Function ShowDateCreated(f
File 对象 提供对文件的所有属性的访问。 说明 以下代码举例说明如何获得一个 File 对象并查看它的属性: Function ShowDateCreated(fil
Drive 对象 提供对磁盘驱动器或网络共享的属性的访问。 说明 以下代码举例说明如何使用 Drive 对象访问驱动器的属性: Function ShowFreeSpac
FileSystemObject 对象 提供对计算机文件系统的访问。 说明 以下代码举例说明如何使用 FileSystemObject 对象返回一个 TextStream 对象,此对象可以被读
我是 javascript OOP 的新手,我认为这是一个相对基本的问题,但我无法通过搜索网络找到任何帮助。我是否遗漏了什么,或者我只是以错误的方式解决了这个问题? 这是我的示例代码: functio
我可以很容易地创造出很多不同的对象。例如像这样: var myObject = { myFunction: function () { return ""; } };
function Person(fname, lname) { this.fname = fname, this.lname = lname, this.getName = function()
任何人都可以向我解释为什么下面的代码给出 (object, Object) 吗? (console.log(dope) 给出了它应该的内容,但在 JSON.stringify 和 JSON.parse
我正在尝试完成散点图 exercise来自免费代码营。然而,我现在只自己学习了 d3 几个小时,在遵循 lynda.com 的教程后,我一直在尝试确定如何在工具提示中显示特定数据。 This code
我是一名优秀的程序员,十分优秀!