我正在尝试使用IntelliJ中的Java swing编写程序。我设计了一个带有IntelliJ拖放用户界面的GUI。但是,我的Jlist没有出现在我的Jscrollpane中。 IntelliJ说我的框架没有GridManager,但我尝试添加一个GridManager,它搞砸了我的GUI格式。有人能帮助我吗?谢谢!
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
public class GUIF {
private JButton button5;
private JList list1;
private JButton button1;
private JButton button4;
private JButton button2;
private JButton button3;
private JTextField textField1;
private JPanel Panel;
private JScrollPane scrollpane1;
public GUIF() {
this.listOps();
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String data = "";
if (list1.getSelectedIndex() != -1) {
data = "Item selected: " + list1.getSelectedValue();
textField1.setText(data);
}
textField1.setText(data);
}
});
}
private void prepGUI() {
JFrame frame = new JFrame("Store");
frame.setContentPane(new GUIF().Panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
private void listOps(){
DefaultListModel<String> items = new DefaultListModel<>();
items.addElement("chocolate");
items.addElement("milk");
list1 = new JList(items);
list1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list1.setSelectedIndex(0);
list1.setVisibleRowCount(10);
list1.setVisible(true);
scrollpane1.add(list1);
scrollpane1.setVisible(true);
}
public static void main(String[] args) {
GUIF guif = new GUIF();
guif.prepGUI();
}
}