Java 如何将值JList项从JFrame传递到另一个JFrame(仅值)?
我写了一个有两个班的测试。 第一个JPanel,手势:JFrame with jlist+按钮(按钮打开jlist 2,PanelTest) 第二个JPanel,PanelTest:JFrame和我想在字符串中恢复JFrame手势(JList)中的select value项 怎么做 Gestion.java:Java 如何将值JList项从JFrame传递到另一个JFrame(仅值)?,java,swing,jframe,Java,Swing,Jframe,我写了一个有两个班的测试。 第一个JPanel,手势:JFrame with jlist+按钮(按钮打开jlist 2,PanelTest) 第二个JPanel,PanelTest:JFrame和我想在字符串中恢复JFrame手势(JList)中的select value项 怎么做 Gestion.java: package IHM; import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.List;
package IHM;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.List;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.DocumentListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.JList;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;
public class Gestion extends JFrame {
private DocumentListener myListener;
public String test;
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Gestion frame = new Gestion();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public String getTest() {
return test;
}
/**
* Create the frame.
* @throws Exception
*/
public Gestion() throws Exception {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
final PanelTest panel2 = new PanelTest();
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.NORTH);
String choix[] = {" Pierre", " Paul", " Jacques", " Lou", " Marie"};
final JList list = new JList(choix);
panel.add(list);
list.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent arg0) {
test = (String) list.getSelectedValue();
System.out.println(test);
// PanelTest.setValue(test);
}
});
JPanel panel_1 = new JPanel();
contentPane.add(panel_1, BorderLayout.SOUTH);
JButton btnNewButton = new JButton("New button");
btnNewButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new PanelTest().setVisible(true);
fermerFenetre();
}
});
panel_1.add(btnNewButton);
}
public void fermerFenetre(){
this.setVisible(false);
}
}
PanelTest.java
package IHM;
import java.awt.BorderLayout;
public class PanelTest extends JFrame {
public String tyty;
private JPanel contentPane;
private JTextField textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
PanelTest frame = new PanelTest();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public PanelTest() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
textField = new JTextField();
contentPane.add(textField, BorderLayout.WEST);
textField.setColumns(10);
}
}
建议:
- 将列表变量设置为字段,而不是局部变量,或者将其设置为最终局部变量,以便可以在匿名ActionListener的内部访问
- 在启动第二个窗口的ActionListener中获取所选列表项
- 通过字符串参数将该字符串传递到PanelTest对象中
- 第二个窗口应该是一个对话框,比如JDialog,而不是JFrame
- 另外,您很少希望GUI类扩展顶级窗口,如JFrames或JDialogs,因为这大大限制了GUI代码的灵活性
比如说,
import java.awt.Component;
import java.awt.Dialog.ModalityType;
import java.awt.Window;
import java.awt.event.ActionEvent;
import javax.swing.*;
@SuppressWarnings("serial")
public class Gestion2 extends JPanel {
private static final String CHOIX[] = { " Pierre", " Paul", " Jacques",
" Lou", " Marie" };
private JList<String> choixList = new JList<>(CHOIX);
public Gestion2() {
JPanel listPanel = new JPanel();
listPanel.add(new JScrollPane(choixList));
JPanel btnPanel = new JPanel();
btnPanel.add(new JButton(new ListSelectAction("Select Item and Press")));
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
add(listPanel);
add(btnPanel);
}
private class ListSelectAction extends AbstractAction {
public ListSelectAction(String name) {
super(name);
}
@Override
public void actionPerformed(ActionEvent e) {
String selectedItem = choixList.getSelectedValue();
if (selectedItem != null) {
PanelTest2 panelTest2 = new PanelTest2(selectedItem);
Component component = (Component) e.getSource();
Window win = SwingUtilities.getWindowAncestor(component);
// JOptionPane example
JOptionPane.showMessageDialog(win, panelTest2,
"JOptionPane Example", JOptionPane.PLAIN_MESSAGE);
// or JDialog example
JDialog dialog = new JDialog(win, "JDialog Example",
ModalityType.APPLICATION_MODAL);
dialog.add(panelTest2);
dialog.pack();
dialog.setLocationRelativeTo(win);
dialog.setVisible(true);
}
}
}
private static void createAndShowGui() {
Gestion2 mainPanel = new Gestion2();
JFrame frame = new JFrame("Gestion2");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
@SuppressWarnings("serial")
class PanelTest2 extends JPanel {
private String selectedItem;
private JTextField textField = new JTextField(10);
public PanelTest2(String selectedItem) {
this.selectedItem = selectedItem;
textField.setText(selectedItem);
add(new JLabel("Selected Item:"));
add(textField);
}
public String getSelectedItem() {
return selectedItem;
}
}
导入java.awt.Component;
导入java.awt.Dialog.ModalityType;
导入java.awt.Window;
导入java.awt.event.ActionEvent;
导入javax.swing.*;
@抑制警告(“串行”)
公共类Gestion2扩展了JPanel{
私有静态最终字符串CHOIX[]={“Pierre”、“Paul”、“Jacques”,
“卢”、“玛丽”};
私人JList choixList=新JList(CHOIX);
公共手势2(){
JPanel listPanel=newjpanel();
add(新的JScrollPane(choixList));
JPanel btnPanel=新的JPanel();
添加(新的JButton(新的ListSelectAction(“选择项目并按下”));
setLayout(新的BoxLayout(这是BoxLayout.PAGE_轴));
添加(列表面板);
添加(btnPanel);
}
私有类ListSelectAction扩展了AbstractAction{
公共ListSelectAction(字符串名称){
超级(姓名);
}
@凌驾
已执行的公共无效操作(操作事件e){
字符串selectedItem=choixList.getSelectedValue();
如果(selectedItem!=null){
PanelTest2 PanelTest2=新的PanelTest2(selectedItem);
组件=(组件)e.getSource();
Window win=SwingUtilities.getWindow祖先(组件);
//JOptionPane示例
showMessageDialog(win,panelTest2,
“JOptionPane示例”,JOptionPane.PLAIN_消息);
//或JDialog示例
JDialog dialog=newjdialog(win,“JDialog示例”,
ModalityType.APPLICATION_MODAL);
添加(panelTest2);
dialog.pack();
对话框。setLocationRelativeTo(win);
对话框.setVisible(true);
}
}
}
私有静态void createAndShowGui(){
Gestion2主面板=新的Gestion2();
JFrame=新JFrame(“手势2”);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(主面板);
frame.pack();
frame.setLocationByPlatform(真);
frame.setVisible(true);
}
公共静态void main(字符串[]args){
SwingUtilities.invokeLater(新的Runnable(){
公开募捐{
createAndShowGui();
}
});
}
}
@抑制警告(“串行”)
类PanelTest2扩展了JPanel{
私有字符串selectedItem;
私有JTextField textField=新JTextField(10);
公共面板测试2(字符串selectedItem){
this.selectedItem=selectedItem;
textField.setText(selectedItem);
添加(新JLabel(“选定项:”);
添加(文本字段);
}
公共字符串getSelectedItem(){
返回selectedItem;
}
}
感谢您帮助气垫船装载鳗鱼,但我想使用J2框架。有可能吗?Nikolas@NicolasLEFEBVRE:是的,只需将JPanel推入JFrame,但您为什么要这样做?拥有多个主应用程序窗口没有什么意义。@NicolasLEFEBVRE:请查看有关此问题的更多详细信息。我希望得到您的建议:我将eclipse luna与数据库HSQLDB一起使用。我是java初学者。我使用设计模式MVC。我在HSQLDB中有4个表,带有外键和主键。当我在Jlist(或更多项目)部门中选择一个项目时,我更新Jlist课程…当我在课程中选择一个项目时,我更新Jlist学生…:部门->课程->学生->课程成绩表..在使用Jlist的每个JFrame中,我有两个按钮,添加和删除。在最后一张JFrame课程成绩表中,我可以计算学生的平均成绩,我有一个按钮来抵消总体平均成绩。你会怎么做you@NicolasLEFEBVRE:您上面的评论看起来像是一个新问题,因此可能应该作为一个问题来提问。请参阅