0
我需要幫助,通過JOptionPane中添加一個字符串到一個JList這是我到目前爲止的代碼:如何修改/添加一個字符串通過的JOptionPane來的jList
import javax.swing.JPanel;
import java.awt.Dimension;
import java.awt.Color;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JList;
import javax.swing.BoxLayout;
import javax.swing.JScrollPane;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.util.ArrayList;
/**
* Write a description of class Front_Menu here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Front_Menu
{
//Fields
private JFrame frame;
private JButton button1;
private JList list;
private JScrollPane listScroller;
private JPanel panel1;
private String title;
/**
* Construct the GUI
*/
public void go()
{
frame = new JFrame("Flashcard Maker");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension frameSize = new Dimension(400, 300);
frame.getContentPane().setPreferredSize(frameSize);
frame.getContentPane().setBackground(Color.WHITE);
button1 = new JButton("New Set");
button1.addActionListener(new ButtonListener());
frame.getContentPane().add(BorderLayout.SOUTH, button1);
String[] titleArray ={"Biology Set", title};
list = new JList(titleArray);
listScroller = new JScrollPane(list);
frame.getContentPane().add(BorderLayout.CENTER, listScroller);
frame.pack();
frame.setVisible(true);
}
/**
* Main Method
*/
public static void main(String[] args)
{
Front_Menu front_menu = new Front_Menu();
front_menu.go();
}
/**
* Inner class for the Button Listener
*/
private class ButtonListener implements ActionListener
{
/**
* Override the actionPerformed Method
*/
public void actionPerformed(ActionEvent event)
{
title = JOptionPane.showInputDialog(
null,
"What is the title of your new set? "
);
}
}
}
所以在這個程序中,我有一個JList我想通過JOptionPane添加另一個字符串來進行修改。但是,它編譯時,當我點擊我的按鈕時它不會顯示。
感謝先進!
'的getContentPane()加(BorderLayout.SOUTH,按鈕1);'你需要切換argume nts命令,請閱讀該方法的文檔。 – user1803551
@ user1803551也許你應該對['Container#add(String,Component)'](https://docs.oracle.com/javase/8/docs/api/java/awt/Container.html#add-java .lang.String-java.awt.Component-) - 建議不要使用這種方法,但它仍然有效 – MadProgrammer
你是否嘗試給按鈕一個動作命令,只是爲了嘗試一切? –