package me.an.ugm;
import java.awt.EventQueue;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.UIManager;
public class Application
{
private JFrame frame;
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
Application window = new Application();
window.frame.setVisible(true);
} catch (Exception e)
{
e.printStackTrace();
}
}
});
}
public Application()
{
initialize();
}
private void initialize()
{
frame = new JFrame();
frame.setTitle("Application");
frame.setBounds(100, 100, 401, 450);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
//frame.setResizable(false);
frame.setLocationRelativeTo(null);
JPanel panel = new JPanel();
panel.setBounds(0, 0, 296, 710);
panel.setLayout(null);
JScrollPane scrollPane = new JScrollPane(panel, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setBounds(0, 0, 296, 399);
frame.getContentPane().add(scrollPane);
for (int i = 0, j = 10; i < 20; i++, j += 35)
{
JButton button1 = new JButton();
button1.setBounds(10, j, 25, 25);
panel.add(button1);
JComboBox<String> selectorBox = new JComboBox<>();
selectorBox.setBounds(40, j, 200, 25);
panel.add(selectorBox);
JButton button2 = new JButton();
button2.setBounds(245, j, 25, 25);
panel.add(button2);
}
}
}
我不知道为什么滚动条不会显示。JPanel比JScrollPane大,所以我认为它应该显示出来。另外,当我尝试将setPreferredSize用作滚动窗格而不是setBounds或setSize时,则什么也没有显示。该程序的最终目标是在右侧设置一个按钮,以添加另一组按钮(循环中的按钮),这些按钮将用于选择另一项。我希望程序从10行按钮开始,但是我将其设置为20以测试滚动条。是否没有布局是一个问题,还是我将滚动窗格弄乱了?
问题是没有使用正确的Swing布局。
这是我创建的GUI。
我在按钮和组合框中添加了文本,因此GUI看起来更加逼真。我评论了外观和感觉,重点放在GUI上。我更改了类的名称,因为对于为Stack Overflow编写的所有代码,我都有一个测试包。
我用单独的方法创建了按钮JPanel。我使用了GridLayout。这使我可以创建20行的3个Swing组件。这也使我可以稍微间隔一些组件。
我用另一个单独的方法创建了滚动JPanel。关键是为滚动JPanel使用BorderLayout。我将滚动JPanel设置为按钮JPanel的一半大小,以便滚动。您可以根据需要调整此计算。
这是完整的可运行代码。
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class JButtonScrollGUI {
private JFrame frame;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
// UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
new JButtonScrollGUI();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
private String[] greekAlphabet;
public JButtonScrollGUI() {
this.greekAlphabet = new String[] { "alpha", "beta", "gamma", "epsilon", "zeta" };
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setTitle("Application");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createScrollPanel(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createScrollPanel() {
JPanel panel = new JPanel(new BorderLayout());
JPanel innerPanel = createButtonPanel();
Dimension d = innerPanel.getPreferredSize();
d.width += 50;
d.height /= 2;
panel.setPreferredSize(d);
JScrollPane scrollPane = new JScrollPane(innerPanel);
panel.add(scrollPane, BorderLayout.CENTER);
return panel;
}
private JPanel createButtonPanel() {
JPanel panel = new JPanel(new GridLayout(0, 3, 10, 10));
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
for (int i = 0; i < 20; i++) {
JButton button1 = new JButton("Previous " + i);
panel.add(button1);
JComboBox<String> selectorBox = new JComboBox<>(greekAlphabet);
panel.add(selectorBox);
JButton button2 = new JButton("Next " + i);
button2.setPreferredSize(button1.getPreferredSize());
panel.add(button2);
}
return panel;
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句