java scrollpane放按钮_java – 如何在BoxLayout上放置scrollPane?
我想讓我的JFrame有一個BoxLayout能夠有一個滾動窗格,所以我可以繼續添加組,如果它是收據或發票打印機.有沒有辦法添加ScrollPane?
問題:如何將ScrollPane添加到JFrame?
代碼,最初見過here:
import java.awt.EventQueue;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.GroupLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
/** @see https://stackoverflow.com/a/8504753/230513 */
public class GroupPanel extends JPanel {
private static final long serialVersionUID = 1L;
private JLabel label1 = new JLabel("Primary:");
private JTextField field1 = new JTextField(16);
private JLabel label2 = new JLabel("Secondary:");
private JTextField field2 = new JTextField(16);
private JLabel label3 = new JLabel("Tertiary:");
private JTextField field3 = new JTextField(16);
JScrollPane sp = new JScrollPane();
public GroupPanel(int n) {
this.setBorder(BorderFactory.createTitledBorder("Panel " + n));
GroupLayout l = new GroupLayout(this);
this.setLayout(l);
l.setAutoCreateGaps(true);
l.setAutoCreateContainerGaps(true);
l.setHorizontalGroup(l.createSequentialGroup()
.addGroup(l.createParallelGroup(GroupLayout.Alignment.TRAILING)
.addComponent(label1)
.addComponent(label2)
.addComponent(label3))
.addGroup(l.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(field1)
.addComponent(field2)
.addComponent(field3))
);
l.setVerticalGroup(l.createSequentialGroup()
.addGroup(l.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(label1)
.addComponent(field1))
.addGroup(l.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(label2)
.addComponent(field2))
.addGroup(l.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(label3)
.addComponent(field3))
);
sp.setToolTipText("test");
sp.add(this);
}
private static void display() {
JFrame f = new JFrame("GroupPanel");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().setLayout(new BoxLayout(f.getContentPane(), BoxLayout.Y_AXIS));
f.getContentPane().add(new GroupPanel(1));
f.getContentPane().add(new GroupPanel(2));
f.getContentPane().add(new GroupPanel(3));
f.getContentPane().add(new GroupPanel(4));
f.getContentPane().add(Box.createVerticalGlue());
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
display();
}
});
}
}
解決方法:
只需將所有組件首先包含在JPanel中,然后將其添加到JScrollPane中,最后將JScrollPane添加到JFrames內容窗格中.
示例代碼:
JPanel panel = new JPanel();
JScrollPane scrollPane = new JScrollPane(panel);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(new GroupPanel(1));
panel.add(new GroupPanel(2));
panel.add(new GroupPanel(3));
panel.add(new GroupPanel(4));
panel.add(Box.createVerticalGlue());
f.getContentPane().add(scrollPane);
屏幕截圖;
標簽:boxlayout,java,swing,jpanel,jscrollpane
來源: https://codeday.me/bug/20190830/1770715.html
總結
以上是生活随笔為你收集整理的java scrollpane放按钮_java – 如何在BoxLayout上放置scrollPane?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: xjc java_xjc命令转换成jav
- 下一篇: linux 跑天龙八部游戏脚本,求推荐天