[英]JScrollPane in Jlist
hello goodevening to all i have a problem on my program with the ScrollPane in my JList i cant put an JScrollPane in my list because i am using a panel instead of Container this is my code so far its all runnable the problem is if you enter a high number in the number of times the some output will not be able to see because of the size of my list . 大家好,我在JList上使用ScrollPane遇到程序问题,我无法在列表中放置JScrollPane,因为我使用的是面板而不是Container,这是我的代码,到目前为止,所有可运行的问题是如果输入由于我列表的大小,一些输出将无法看到的次数过多。 so this is the code 所以这是代码
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MultCen extends JFrame implements ActionListener
{
public static void main(String args [])
{
MultCen e = new MultCen();
e.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
e.setVisible(true);
e.setSize(300,450);
}
JTextField t1 = new JTextField();
JTextField t2 = new JTextField();
JButton b = new JButton("Okay");
JButton c = new JButton("Clear");
JList list = new JList();
JLabel lab = new JLabel();
DefaultListModel m = new DefaultListModel();
public MultCen()
{
JPanel panel = new JPanel();
panel.setLayout(null);
JLabel l = new JLabel("Enter a number :");
JLabel l1 = new JLabel("How many times :");
l.setBounds(10,10,130,30);
l1.setBounds(10,40,130,30);
t1.setBounds(140,10,130,25);
t2.setBounds(140,40,130,25);
b.setBounds(60,90,75,30);
c.setBounds(150,90,75,30);
list.setBounds(30,140,220,220);
panel.add(t1);
panel.add(t2);
panel.add(l);
panel.add(l1);
panel.add(list);
panel.add(b);
panel.add(c);
getContentPane().add(panel);
b.addActionListener(this);
c.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == b)
{
int t3 = Integer.parseInt(t1.getText());
int t4 = Integer.parseInt(t2.getText());
m.addElement("The multiplication Table of "+t3);
for (int cc =1 ; cc <=t4; cc++ )
{
lab.setText(t3+"*"+cc+" = "+(t3*cc));
m.addElement(lab.getText());
list.setModel(m);
}
}
if(e.getSource() == c)
{
t1.setText("");
t2.setText("");
m.removeAllElements();
}
}
}
JScrollPane
does not work with null
Layout. JScrollPane
不适用于null
布局。 Use BoxLayout
or any other resizeable layout instead. 请改用BoxLayout
或任何其他可调整大小的布局。 This is the limitation of setLayout(null)
. 这是setLayout(null)
的限制。
Use Layout managers. 使用布局管理器。 You've asked a lot of questions here and I'm sure a few you have been advised not to use null layout. 您在这里提出了很多问题,并且我敢肯定,建议您不要使用空布局。 Again here is the tutorial Laying out components Within a container . 此处再次是本教程,将组件放置在容器中 。 Learn to use them so you don't run into the million possible problems on the road ahead. 学会使用它们,这样您就不会在未来的道路上遇到数百万个可能的问题。 This kind of problem being one of them. 这种问题就是其中之一。
here's an example of how you could achieve the same thing with layout managers, and some empty borders for white space. 这是一个示例,说明如何使用布局管理器以及空白的空白框可以实现相同的目的。
With Layout Manager 使用布局管理器
Without Layout Manger 没有布局管理器
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class MultCen extends JFrame {
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
MultCen e = new MultCen();
e.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
e.setVisible(true);
e.pack();
}
});
}
JTextField t1 = new JTextField(10);
JTextField t2 = new JTextField(10);
JButton b = new JButton("Okay");
JButton c = new JButton("Clear");
JLabel lab = new JLabel();
DefaultListModel m = new DefaultListModel();
public MultCen() {
JPanel topPanel = new JPanel(new GridLayout(2, 2, 0, 5));
JLabel l = new JLabel("Enter a number :");
JLabel l1 = new JLabel("How many times :");
topPanel.add(l);
topPanel.add(t1);
topPanel.add(l1);
topPanel.add(t2);
JPanel buttonPanel = new JPanel();
buttonPanel.add(b);
buttonPanel.add(c);
buttonPanel.setBorder(new EmptyBorder(10, 0, 10, 0));
JList list = new JList();
JScrollPane scroll = new JScrollPane(list);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setPreferredSize(new Dimension(300, 300));
JPanel panel = new JPanel(new BorderLayout());
panel.add(topPanel, BorderLayout.NORTH);
panel.add(buttonPanel, BorderLayout.CENTER);
panel.add(scroll, BorderLayout.SOUTH);
panel.setBorder(new EmptyBorder(10, 15, 10, 15));
getContentPane().add(panel);
}
}
Side Notes 旁注
Run Swing apps from the Event Dispatch Thread. 从事件调度线程运行Swing应用程序。 See Initial Threads 请参阅初始线程
When you do decide to use layout managers, just pack()
your frame instead of setSize()
当你决定使用布局管理器,只是pack()
的框架,而不是setSize()
Use better variable names. 使用更好的变量名。
See Extends JFrame vs. creating it inside the the program 请参阅扩展JFrame与在程序内部创建它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.