简体   繁体   中英

Java Swing controlling height of JProgressBar

I want to control the height of a progress bar. I am using the following code:

JPanel panel = new JPanel(new GridLayout(1,2));
JLabel label = new JLabel("Some text");
JPanel jpb = new JPanel(new BorderLayout());    jpb.setBackground(Color.WHITE);
JProgressBar progressBar = new JProgressBar();

//progressBar.setPreferredSize(new Dimension(20,));
progressBar.setForeground(Color.RED);
progressBar.setPreferredSize(new Dimension(20,3));
progressBar.setValue(40);
progressBar.setStringPainted(true);

jpb.add(progressBar, BorderLayout.CENTER);
panel.add(label);
panel.add(jpb);

But the progressbar sticks out like a dragon. Please recommend a solution.

I would suggest to use GridBagLayout.
1. Use a Jpanel and set Borderlayout(already done in your code)
2. Make another JPanel and set GridBagLayout with 1 row and 3 column in it.
3. Post 2nd JPanel at the center of 1st JPanel.
4. Add your JProgressbar to 1st column
5. Set the size of column as per your visual needs for width and height of JProgressbar

Hope this will be helpful. :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM