如何在java程序中加上滚动条,为什么我的程序滚动条没显示?
importjava.awt.*;importjavax.swing.*;publicclassPanelsextendsJFrame{publicPanels(){JP...
import java.awt.*;
import javax.swing.*;
public class Panels extends JFrame {
public Panels() {
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(200, 10));
for (int i = 0; i < 2000; i++) {
p1.add(new JButton(" " + i));
}
JPanel p2 = new JPanel(); //new BorderLayout());
JTextArea txaDisplay = new JTextArea(); //初始化一个滚动条
JScrollPane scroll = new JScrollPane(txaDisplay); //分别设置水平和垂直滚动条自动出现
scroll
.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); //水平
scroll
.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);//垂直
scroll.getViewport().add(txaDisplay);
//JPanel jpanel=new JPanel();
p2.add(p1, BorderLayout.CENTER);
add(p2, BorderLayout.CENTER);
p2.add(scroll);
}
public static void main(String[] args) {
Panels frame = new Panels();
frame.setTitle("可选座位号");
frame.setSize(500, 500); //外框大小
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
} 展开
import javax.swing.*;
public class Panels extends JFrame {
public Panels() {
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(200, 10));
for (int i = 0; i < 2000; i++) {
p1.add(new JButton(" " + i));
}
JPanel p2 = new JPanel(); //new BorderLayout());
JTextArea txaDisplay = new JTextArea(); //初始化一个滚动条
JScrollPane scroll = new JScrollPane(txaDisplay); //分别设置水平和垂直滚动条自动出现
scroll
.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); //水平
scroll
.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);//垂直
scroll.getViewport().add(txaDisplay);
//JPanel jpanel=new JPanel();
p2.add(p1, BorderLayout.CENTER);
add(p2, BorderLayout.CENTER);
p2.add(scroll);
}
public static void main(String[] args) {
Panels frame = new Panels();
frame.setTitle("可选座位号");
frame.setSize(500, 500); //外框大小
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询