我想将数据库的名称从一类转移到另一类,但是从JList提取名称时遇到问题。在没有线程的情况下,在MenuTest类中接收db名称的最简单方法是什么?

这是我的代码:

import javax.swing.JButton;
import javax.swing.JFrame;

import javax.swing.JPanel;

public class MenuGUI extends JFrame
{
    public MenuGUI()
    {
        setTitle("Aplication");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(600,600);
        setLayout(null);
        setVisible(true);
    }

    public void addPanel(JPanel jPanel, int a, int b, int w, int h)
    {
        jPanel.setBounds(a, b, w, h);
        add(jPanel);
    }

    public void addButton(JButton jButton, int a, int b, int w, int h)
    {
        jButton.setBounds(a,b,w,h);
        add(jButton);
    }
}

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;

public class MainWindow
{
    private MenuGUI menuGui;
    private JButton jButtonChooseNameDB;
    public NameDataBasePanel _name;

    public MainWindow()
    {
        menuGui = new MenuGUI();
        addComponents();
    }

    public void addComponents()
    {
        jButtonChooseNameDB = new JButton("Choose name of database");
        menuGui.addButton(jButtonChooseNameDB, 50, 10, 200, 30);
        addListeners();
    }

    public void addListeners()
    {
        jButtonChooseNameDB.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
            _name = new nameDataBasePanel();
            }
        });
    }
}

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class NameDataBasePanel extends JDialog implements ListSelectionListener
{
    private JFrame jFrame;
    private JList jList;
    private JPanel jPanel;
    private String[] list;
    private String dataBaseName;
    private boolean flag = false;

    public NameDataBasePanel()
    {
        list = new String[2];
        list[0] = "database1";
        list[1] = "database2";

        jFrame = new JFrame();
        jFrame.setSize(300,300);
        jFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        jFrame.setVisible(true);
        jFrame.setLayout(null);

        jPanel = new JPanel();
        jPanel.setBounds(10, 10, 200, 200);
        jPanel.setVisible(true);
        jList = new JList(list);
        jList.setLayoutOrientation(JList.HORIZONTAL_WRAP);

        jList.addListSelectionListener(this);
        jPanel.add(jList);
        jFrame.add(jPanel);
    }

    @Override
    public void valueChanged(ListSelectionEvent e)
    {
        if(e.getValueIsAdjusting())
        {
            dataBaseName = (String) jList.getSelectedValue();
            flag = true;
        }

    }

    public String getDataBaseName()
    {
        return dataBaseName;
    }

    public boolean isFlag()
    {
        return flag;
    }
}


public class MenuTest
{

    public static void main(String[] args)
    {
        MainWindow mainWindow = new MainWindow();
        System.out.println(mainWindow._name.getDataBaseName());
        }
}

最佳答案

here所示,使用observer pattern。让MainWindow收听NameDataBasePanel以获取合适的PropertyChangeEvent。另请参见使用无模式对话框的相关example

07-28 01:19