JList 列表项被压扁
JList list items squashed
我的 JList 可以正常工作,但只有第一个列表项的大小合适。它下面的所有其他东西都被压扁了。我已经搜索了所有 Whosebug 并没有找到太多(让它工作虽然都是 Whosebug)
package networks;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.*;
/**
*
* @author Matthew
*/
public class Networks extends JFrame implements ActionListener {
private static JPanel container = new JPanel();
private static JPanel graphPanel = new JPanel();
private static JPanel content = new JPanel();
private static JPanel buttons = new JPanel();
private static Graphics2D g;
private static JButton read = new JButton("Read");
private static JButton breadth = new JButton("Breadth");
private static JButton depth = new JButton("Depth");
private String[] listModel = new String[10];
private static JScrollPane scroller = new JScrollPane();
private JList lists;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Networks networks = new Networks();
networks.setVisible(true);
}
public Networks() {
super("Station Networks");
//Set up window and frames, buttons and lists
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
content.setPreferredSize(new Dimension(260,300));
buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS));
graphPanel.setPreferredSize(new Dimension(255,255));
//Add default value, tester
listModel[0] = "The station list is currently empty";
lists = new JList(listModel);
lists.setVisibleRowCount(5);
scroller.setViewportView(lists);
BufferedImage im = new BufferedImage(255,255,BufferedImage.TYPE_INT_RGB);
g = im.createGraphics();
//add listeners
read.addActionListener(this);
breadth.addActionListener(this);
depth.addActionListener(this);
//put it all togethe
content.add(graphPanel);
content.add(new Label("List"));
content.add(scroller);
buttons.add(read);
buttons.add(breadth);
buttons.add(depth);
container.add(content);
container.add(buttons);
add(container);
pack();
setVisible(true);
}
我有一个稍后添加列表项的功能。这些物品被压扁了。有没有办法设置列表项的大小?
不要为数据使用数组。您的数组包含空值,第一个条目除外。空条目变得有趣。
而是使用 DefaultListModel
并直接向其中添加项目:
DefaultList model = new DefaultListModel();
model.addElement( "line1" );
JList list = new JList( model );
我的 JList 可以正常工作,但只有第一个列表项的大小合适。它下面的所有其他东西都被压扁了。我已经搜索了所有 Whosebug 并没有找到太多(让它工作虽然都是 Whosebug)
package networks;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.*;
/**
*
* @author Matthew
*/
public class Networks extends JFrame implements ActionListener {
private static JPanel container = new JPanel();
private static JPanel graphPanel = new JPanel();
private static JPanel content = new JPanel();
private static JPanel buttons = new JPanel();
private static Graphics2D g;
private static JButton read = new JButton("Read");
private static JButton breadth = new JButton("Breadth");
private static JButton depth = new JButton("Depth");
private String[] listModel = new String[10];
private static JScrollPane scroller = new JScrollPane();
private JList lists;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Networks networks = new Networks();
networks.setVisible(true);
}
public Networks() {
super("Station Networks");
//Set up window and frames, buttons and lists
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
content.setPreferredSize(new Dimension(260,300));
buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS));
graphPanel.setPreferredSize(new Dimension(255,255));
//Add default value, tester
listModel[0] = "The station list is currently empty";
lists = new JList(listModel);
lists.setVisibleRowCount(5);
scroller.setViewportView(lists);
BufferedImage im = new BufferedImage(255,255,BufferedImage.TYPE_INT_RGB);
g = im.createGraphics();
//add listeners
read.addActionListener(this);
breadth.addActionListener(this);
depth.addActionListener(this);
//put it all togethe
content.add(graphPanel);
content.add(new Label("List"));
content.add(scroller);
buttons.add(read);
buttons.add(breadth);
buttons.add(depth);
container.add(content);
container.add(buttons);
add(container);
pack();
setVisible(true);
}
我有一个稍后添加列表项的功能。这些物品被压扁了。有没有办法设置列表项的大小?
不要为数据使用数组。您的数组包含空值,第一个条目除外。空条目变得有趣。
而是使用 DefaultListModel
并直接向其中添加项目:
DefaultList model = new DefaultListModel();
model.addElement( "line1" );
JList list = new JList( model );