我正在制作一个下载器,目前它的界面如下所示:
JFrame使用了BorderLayout布局。在NORTH区域,我有一个JPanel(FlowLayout)。在SOUTH区域,也有一个JPanel(FlowLayout)。在WEST区域,我只有一个带有滚动条的JTextArea。这些都显示正确。但是,在EAST区域,我目前有一个JPanel(GridLayout(10,1))。
我想在EAST区域显示最多10个JProgressBar,并且可以动态地添加和删除。问题是,我无法使它们看起来像我想要的:我希望JProgressBars的宽度填满整个EAST区域,因为1)这会使应用程序更对称,2)进度条可能包含当前不适合的长字符串。我尝试将包含GridLayout(10,1)的JPanel放入FlowLayout中,然后将该FlowLayout放入EAST区域,但这也没有起作用。
我的代码(SSCCE)如下:
我想在EAST区域显示最多10个JProgressBar,并且可以动态地添加和删除。问题是,我无法使它们看起来像我想要的:我希望JProgressBars的宽度填满整个EAST区域,因为1)这会使应用程序更对称,2)进度条可能包含当前不适合的长字符串。我尝试将包含GridLayout(10,1)的JPanel放入FlowLayout中,然后将该FlowLayout放入EAST区域,但这也没有起作用。
我的代码(SSCCE)如下:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
new DownloadFrame();
}
private static class DownloadFrame extends JFrame {
private JButton downloadButton;
private JTextField threadIdTextField;
private JTextArea downloadStatusTextArea;
private JScrollPane scrollPane;
private JTextField downloadLocationTextField;
private JButton downloadLocationButton;
private JPanel North;
private JPanel South;
private JPanel ProgressBarPanel;
private Map<String, JProgressBar> progressBarMap;
public DownloadFrame() {
InitComponents();
InitLayout();
AddComponents();
AddActionListeners();
setVisible(true);
setSize(700, 300);
}
private void InitComponents() {
downloadButton = new JButton("Dowload");
threadIdTextField = new JTextField(6);
downloadStatusTextArea = new JTextArea(10, 30);
scrollPane = new JScrollPane(downloadStatusTextArea, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
downloadLocationTextField = new JTextField(40);
downloadLocationButton = new JButton("...");
North = new JPanel();
South = new JPanel();
ProgressBarPanel = new JPanel();
progressBarMap = new HashMap<String, JProgressBar>();
}
private void InitLayout() {
North.setLayout(new FlowLayout());
South.setLayout(new FlowLayout());
ProgressBarPanel.setLayout(new GridLayout(10, 1));
}
private void AddComponents() {
North.add(threadIdTextField);
North.add(downloadButton);
add(North, BorderLayout.NORTH);
add(ProgressBarPanel, BorderLayout.EAST);
South.add(downloadLocationTextField);
South.add(downloadLocationButton);
add(South, BorderLayout.SOUTH);
add(scrollPane, BorderLayout.WEST);
}
private void AddActionListeners() {
downloadButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addNewProgessBar(threadIdTextField.getText());
}
});
}
public void addNewProgessBar(String threadId) {
JProgressBar progressBar = new JProgressBar();
progressBar.setStringPainted(true);
progressBarMap.put(threadId, progressBar);
drawProgessBars();
}
void drawProgessBars() {
ProgressBarPanel.removeAll();
for (JProgressBar progressBar : progressBarMap.values()) {
ProgressBarPanel.add(progressBar);
}
validate();
repaint();
}
}
}
Thanks in advance.
EDIT
Easiest solution: change
add(ProgressBarPanel, BorderLayout.EAST);
为了
add(ProgressBarPanel, BorderLayout.CENTER);
camelCase
,而不是EachWordUpperCase
。 - Andrew Thompson