将 Jpanel 添加到 Jscrollbar
Add Jpanel to a Jscrollbar
我正在尝试将 Jpanel 添加到 JscrollBAr,但是当我 运行 java 它不起作用时。这是一个带有问题的 RadioButton。你能帮我吗 ?
这是我的 class jpanel :
public class yesnoPanel extends JPanel {
private JPanel quest;
private JPanel answ;
public yesnoPanel(String q){
JPanel pan = new JPanel();
JRadioButton yes = new JRadioButton("yes");
JRadioButton no = new JRadioButton("no");
ButtonGroup bg = new ButtonGroup();
JTextArea question = new JTextArea(1,20);
question.setText(q);
JPanel pan2 = new JPanel();
pan2.add(question);
bg.add(yes);
bg.add(no);
pan.add(yes);
pan.add(no);
this.quest=pan2;
this.answ=pan;
}
我可以在使用 JFrame 时显示它们,但不能使用 Jscrollbar
这是我主要的 jscrollbar :
public static void main(String[] args) {
JFrame fram = new JFrame();
yesnoPanel question = new yesnoPanel("are you ok");
JPanel q = question.getQuestion();
JPanel a = question.getAnsw();
JPanel all = new JPanel();
all.add(q);
all.add(a);
yesnoPanel question2 = new yesnoPanel("sure ?");
JPanel q2 = question2.getQuestion();
q2.setSize(200,500);
JPanel a2 = question2.getAnsw();
JPanel all2 = new JPanel();
all2.add(q2,BorderLayout.WEST);
all2.add(a2);
JScrollPane scroll = new JScrollPane();
scroll.setPreferredSize(new Dimension(100,100));
scroll.setViewportView(all);
scroll.setVisible(true);
}
感谢大家
用这个替换 main 方法中的最后几行以使其工作。
JPanel master = new JPanel();
master.add(all);
master.add(all2);
JScrollPane scroll = new JScrollPane(master);
scroll.setPreferredSize(new Dimension(100,100));
scroll.setViewportView(all);
scroll.setVisible(true);
fram.add(scroll);
fram.pack();
fram.setVisible(true);
fram.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
我正在尝试将 Jpanel 添加到 JscrollBAr,但是当我 运行 java 它不起作用时。这是一个带有问题的 RadioButton。你能帮我吗 ? 这是我的 class jpanel :
public class yesnoPanel extends JPanel {
private JPanel quest;
private JPanel answ;
public yesnoPanel(String q){
JPanel pan = new JPanel();
JRadioButton yes = new JRadioButton("yes");
JRadioButton no = new JRadioButton("no");
ButtonGroup bg = new ButtonGroup();
JTextArea question = new JTextArea(1,20);
question.setText(q);
JPanel pan2 = new JPanel();
pan2.add(question);
bg.add(yes);
bg.add(no);
pan.add(yes);
pan.add(no);
this.quest=pan2;
this.answ=pan;
}
我可以在使用 JFrame 时显示它们,但不能使用 Jscrollbar 这是我主要的 jscrollbar :
public static void main(String[] args) {
JFrame fram = new JFrame();
yesnoPanel question = new yesnoPanel("are you ok");
JPanel q = question.getQuestion();
JPanel a = question.getAnsw();
JPanel all = new JPanel();
all.add(q);
all.add(a);
yesnoPanel question2 = new yesnoPanel("sure ?");
JPanel q2 = question2.getQuestion();
q2.setSize(200,500);
JPanel a2 = question2.getAnsw();
JPanel all2 = new JPanel();
all2.add(q2,BorderLayout.WEST);
all2.add(a2);
JScrollPane scroll = new JScrollPane();
scroll.setPreferredSize(new Dimension(100,100));
scroll.setViewportView(all);
scroll.setVisible(true);
}
感谢大家
用这个替换 main 方法中的最后几行以使其工作。
JPanel master = new JPanel();
master.add(all);
master.add(all2);
JScrollPane scroll = new JScrollPane(master);
scroll.setPreferredSize(new Dimension(100,100));
scroll.setViewportView(all);
scroll.setVisible(true);
fram.add(scroll);
fram.pack();
fram.setVisible(true);
fram.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);