[英]Add a JScrollPane to a JList
I have the following code: Main: 我有以下代码:Main:
package PackageMain;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
public class Main {
public static JFrame frame = new JFrame("Window");
public static PanelOne p1;
public static PanelTwo p2;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setBounds(100, 100, 800, 600);
p1 = new PanelOne();
p2 = new PanelTwo();
frame.setVisible(true);
} catch(Exception e){
}
}
});
}
And class 2: 第二课:
package PackageMain;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class PanelOne{
public PanelOne(){
loadScreen();
}
public void loadScreen(){
JPanel p1 = new JPanel();
DefaultListModel model = new DefaultListModel<String>();
JList list = new JList<String>(model);
//
JScrollPane scroll = new JScrollPane(list);
list.setPreferredSize(null);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
scroll.setViewportView(list);
//
list.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent arg0) {
System.out.println("You selected " + list.getSelectedValue());
}
});
p1.add(list);
Main.frame.add(p1);
Main.frame.revalidate();
Main.frame.repaint();
for (int i = 0; i < 100; i++){
model.addElement("test");
}
}
I've tried a bunch of stuff to get the JScrollPane to appear on the JList, but it doesn't want to. 我已经尝试了很多方法使JScrollPane出现在JList上,但它并不想这样做。 My best guess is that the model is screwing things up, but this is a simplified version, and the model needs to be there. 我最好的猜测是该模型正在搞砸,但这是一个简化的版本,因此模型必须存在。
You're adding the list to too many components: to the JScrollPane's viewport -- OK, but also to the p1 JPanel -- not OK. 您要添加的列表太多的组件:到JScrollPane的视口- OK, 而且对P1的JPanel -也不行。 Add it only to the viewport, and then add the JScrollPane to the GUI (p1 if need be). 仅将其添加到视口,然后将JScrollPane添加到GUI(如果需要,则添加p1)。
Also: 也:
list.setPreferredSize(null);
???? ???? JScrollPane scroll = new JScrollPane(list);
You add the JList to the JScrollPane which is correct. 您将JList添加到JScrollPane是正确的。
p1.add(list);
But then you add the JList
to the JPanel , which is incorrect. A component can only have a single parent, so the
但是随后您将JList
添加到JPanel中, which is incorrect. A component can only have a single parent, so the
, which is incorrect. A component can only have a single parent, so the
JList is removed from the
JScrollPane`. , which is incorrect. A component can only have a single parent, so the
JList is removed from the
JScrollPane中is removed from the
。
You need to add the JScrollPane
to the JPanel
: 您需要将JScrollPane
添加到JPanel
:
p1.add( scroll );
Just add Scroll Pane
to the frame rather than the List
. 只需将“ Scroll Pane
”添加到框架而不是“ List
。
change your line with the below code: 使用以下代码更改行:
Main.frame.add(scroll);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.