java设置的滚动条无法显示,新手学习,请指教
packagecreate;importjava.awt.*;importjavax.swing.*;importjava.awt.event.*;classMyDial...
package create;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class MyDialog extends JDialog{
public MyDialog(dialog frame){
super(frame,"JDialog",true);
Container container=getContentPane();
JLabel jl=new JLabel("这是一个窗体");
container.add(jl);
setBounds(150,150,300,300);
}
}
public class dialog extends JFrame{
public static void main(String[] args) {
new dialog();
}
public dialog(){
Container container=getContentPane();
container.setLayout(null);
JLabel jl=new JLabel("这是一个窗体");
container.add(jl);
JButton jb=new JButton("弹出");
jb.setBounds(50,10,100,21);
jb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
new MyDialog(dialog.this).setVisible(true);
}
});
JLabel jl1=new JLabel("label1");
JRadioButton jr1=new JRadioButton("1");
JRadioButton jr2=new JRadioButton("2");
JRadioButton jr3=new JRadioButton("3");
ButtonGroup group =new ButtonGroup();
JTextArea jt=new JTextArea(50,50); //在这里设置滚动条,却无法显示
JScrollPane js=new JScrollPane(jt);
container.add(js);
container.add(jl1);
container.add(jb);
container.add(jr1);
container.add(jr2);
container.add(jr3);
group.add(jr1);
group.add(jr2);
group.add(jr3);
jl1.setBounds(10, 10, 40, 20);
jr1.setBounds(60,60,60,30);
jr2.setBounds(60,100,60,30);
jr3.setBounds(60,140,60,30);
jl.setBounds(200,30,100,100);
setSize(350,350);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
} 展开
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class MyDialog extends JDialog{
public MyDialog(dialog frame){
super(frame,"JDialog",true);
Container container=getContentPane();
JLabel jl=new JLabel("这是一个窗体");
container.add(jl);
setBounds(150,150,300,300);
}
}
public class dialog extends JFrame{
public static void main(String[] args) {
new dialog();
}
public dialog(){
Container container=getContentPane();
container.setLayout(null);
JLabel jl=new JLabel("这是一个窗体");
container.add(jl);
JButton jb=new JButton("弹出");
jb.setBounds(50,10,100,21);
jb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
new MyDialog(dialog.this).setVisible(true);
}
});
JLabel jl1=new JLabel("label1");
JRadioButton jr1=new JRadioButton("1");
JRadioButton jr2=new JRadioButton("2");
JRadioButton jr3=new JRadioButton("3");
ButtonGroup group =new ButtonGroup();
JTextArea jt=new JTextArea(50,50); //在这里设置滚动条,却无法显示
JScrollPane js=new JScrollPane(jt);
container.add(js);
container.add(jl1);
container.add(jb);
container.add(jr1);
container.add(jr2);
container.add(jr3);
group.add(jr1);
group.add(jr2);
group.add(jr3);
jl1.setBounds(10, 10, 40, 20);
jr1.setBounds(60,60,60,30);
jr2.setBounds(60,100,60,30);
jr3.setBounds(60,140,60,30);
jl.setBounds(200,30,100,100);
setSize(350,350);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
} 展开
1个回答
展开全部
原因是你设置的布局是null,还没有确定JScrollPane的位置,所以不显示,看代码添加一行。另外类名开头大写字母。
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class MyDialog extends JDialog {
public MyDialog(Dialog frame) {
super(frame, "JDialog", true);
Container container = getContentPane();
JLabel jl = new JLabel("这是一个窗体");
container.add(jl);
setBounds(150, 150, 300, 300);
}
}
public class Dialog extends JFrame {//类名命名首字母大写,好的书写规范
public static void main(String[] args) {
new Dialog();
}
public Dialog() {
Container container = getContentPane();
container.setLayout(null);
JLabel jl = new JLabel("这是一个窗体");
container.add(jl);
JButton jb = new JButton("弹出");
jb.setBounds(50, 10, 100, 21);
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new MyDialog(Dialog.this).setVisible(true);
}
});
JLabel jl1 = new JLabel("label1");
JRadioButton jr1 = new JRadioButton("1");
JRadioButton jr2 = new JRadioButton("2");
JRadioButton jr3 = new JRadioButton("3");
ButtonGroup group = new ButtonGroup();
JTextArea jt = new JTextArea(50, 50); // 在这里设置滚动条,却无法显示
JScrollPane js = new JScrollPane(jt);
js.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
js.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
container.add(js);
container.add(jl1);
container.add(jb);
container.add(jr1);
container.add(jr2);
container.add(jr3);
group.add(jr1);
group.add(jr2);
group.add(jr3);
jl1.setBounds(10, 10, 40, 20);
jr1.setBounds(60, 60, 60, 30);
jr2.setBounds(60, 100, 60, 30);
jr3.setBounds(60, 140, 60, 30);
jl.setBounds(200, 30, 100, 100);
js.setBounds(200, 100, 100, 100);//添加这一行
setSize(350, 350);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |