gpt4 book ai didi

Java GUI 自动调整大小

转载 作者:行者123 更新时间:2023-12-03 07:05:33 24 4
gpt4 key购买 nike

这里是绘制 GUI 的地方(注意,该类扩展了 JFrame)。

public Cache() {
SubstanceColorChooserUI col = new SubstanceColorChooserUI();
while (mode == 0);
setResizable(false);
setTitle("Cache");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 483, 374);
setLocationRelativeTo(null);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout());

textField = new JTextField();
textField.setBounds(10, 11, 328, 20);
contentPane.add(textField);
textField.setColumns(10);

JButton btnLoadCache = new JButton("Load cache");
btnLoadCache.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
String loc = textField.getText();
if (loc.equals("")) {
JOptionPane.showMessageDialog(Cache.this, "Please specify a location for the cache.", "Unable to load", JOptionPane.ERROR_MESSAGE);
return;
}
if (!loc.endsWith("\\"))
loc = loc + "\\";
cache = new Store(loc);
loadImages();
} catch (Exception e) {
JOptionPane.showMessageDialog(Cache.this, "Cache failed to initialize.\n" + e.getMessage(), "Unable to load", JOptionPane.ERROR_MESSAGE);
}
}
});
btnLoadCache.setBounds(351, 9, 112, 23);
contentPane.add(btnLoadCache);

JSplitPane splitPane = new JSplitPane();
splitPane.setDividerLocation(150);
splitPane.setContinuousLayout(true);
splitPane.setBounds(10, 44, 453, 279);
contentPane.add(splitPane);

JPanel panellie = new JPanel();
panellie.setLayout(new BorderLayout(0, 0));
panel = new ImagePanel(null);

scrollPane_1 = new JScrollPane();
panellie.add(scrollPane_1, "Center");

scrollPane_1.setViewportView(panel);
splitPane.setRightComponent(panellie);

JPanel panel_1 = new JPanel();
splitPane.setLeftComponent(panel_1);
panel_1.setLayout(new BorderLayout(0, 0));

JScrollPane scrollPane = new JScrollPane();
panel_1.add(scrollPane, BorderLayout.CENTER);

model = new DefaultListModel<ListedImage>();
list = new JList<ListedImage>(model);
list.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent arg0) {
if (arg0.getValueIsAdjusting())
return;
ListedImage img = list.getModel().getElementAt(list.getSelectedIndex());
panel.setImage(img.getImage());
}
});
scrollPane.setViewportView(list);

progressBar = new JProgressBar();
progressBar.setStringPainted(true);
progressBar.setDoubleBuffered(true);
progressBar.setBounds(10, 328, 453, 14);
contentPane.add(progressBar);

}

当我将Resizeable设置为true并开始将程序拖动到更大和更小时时,我该如何做到这一点,如何使框架内的组件(按钮,标签等)在整个过程中调整大小框架正在调整大小

最佳答案

使用布局管理器而不是为每个组件设置边界。

您希望组件移动的方式因程序而异。

看看this ,并尝试看看哪种布局最适合您。

关于Java GUI 自动调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11566974/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com