2017-04-14 55 views
1

我想创建一个JFrame,我只是找到了完美的框架。 我要重新创建:如何重新创建基于JFrame的进度条?

enter image description here

代码即可获得该框架是这样的:

progressBar = new JProgressBar(); 
statusLbl = new JLabel(); 
statusLbl1 = new JLabel(); 
percentLbl = new JLabel(); 

setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
setTitle("Auto-Updater"); 
addWindowListener(new WindowAdapter() { 
    public void windowClosing(WindowEvent evt) { 
    formWindowClosing(evt); 
    } 
}); 
statusLbl.setText("Status:"); 
statusLbl1.setText("N/A"); 
percentLbl.setText("0%"); 
GroupLayout layout = new GroupLayout(getContentPane()); 
getContentPane().setLayout(layout); 

layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(
    layout.createSequentialGroup() 
    .addContainerGap() 
    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) 
     .addGroup(layout.createSequentialGroup() 
      .addComponent(statusLbl).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) 
      .addComponent(statusLbl1).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 304, 32767) 
      .addComponent(percentLbl)).addComponent(progressBar, GroupLayout.Alignment.TRAILING, -1, 380, 32767)) 
    .addContainerGap())); 

layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(
    layout.createSequentialGroup() 
    .addContainerGap() 
    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING, false) 
     .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) 
      .addComponent(statusLbl1, -1, -1, 32767) 
      .addComponent(percentLbl)) 
     .addComponent(statusLbl, -1, -1, 32767)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) 
    .addComponent(progressBar, -2, 30, -2).addContainerGap(-1, 32767))); 

pack(); 

但是我觉得这个长相丑陋,具有零可读性,所以我要问你:如何使用不同的布局重新创建此框架,或者如何以不同的布局来使用此布局以使其可读?

+0

你是什么意思与“如果你改变了状态:N/A到的TitledBorder” – xX4m4zingXx

+0

嗯,我刚刚发现的TitledBorder是什么,但我真的很喜欢的设计现在JFrame就像它一样。我可以将百分比放在进度栏中。是否可以通过任何布局来制作此JFrame? – xX4m4zingXx

+0

我很抱歉浪费你的时间。在回复您的评论之前,我应该查看它。 – xX4m4zingXx

回答

3

如果进度条可以包含在进度条内,则可以使用单个JPanelBorderLayout完成。将标签放入PAGE_START,并将进度条放入CENTER(如果未指定约束,则为默认值)。

注:我倾向于显示在JDialogJOptionPane而非JFrame该面板,但这里是一个基于帧的版本。

enter image description here

import java.awt.*; 
import javax.swing.*; 
import javax.swing.border.EmptyBorder; 

public class ProgressBarPanel { 

    private JComponent ui = null; 

    ProgressBarPanel() { 
     initUI(); 
    } 

    public void initUI() { 
     if (ui!=null) return; 

     ui = new JPanel(new BorderLayout(4,4)); 
     ui.setBorder(new EmptyBorder(4,4,4,4)); 

     JProgressBar progressBar = new JProgressBar(0, 100) { 
      public Dimension getPreferredSize() { 
       // base this on a multiple of the default preferred size to 
       // account for the size of the font used to paint the 
       // progress string 
       return new Dimension(400,40); 
      } 
     }; 
     progressBar.setValue(50); 
     progressBar.setStringPainted(true); 
     ui.add(progressBar); 

     ui.add(new JLabel("Status: N/A"), BorderLayout.PAGE_START); 
    } 

    public JComponent getUI() { 
     return ui; 
    } 

    public static void main(String[] args) { 
     Runnable r = new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (Exception useDefault) { 
       } 
       ProgressBarPanel o = new ProgressBarPanel(); 

       JFrame f = new JFrame(o.getClass().getSimpleName()); 
       f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
       f.setLocationByPlatform(true); 

       f.setContentPane(o.getUI()); 
       f.pack(); 
       f.setMinimumSize(f.getSize()); 

       f.setVisible(true); 
      } 
     }; 
     SwingUtilities.invokeLater(r); 
    } 
} 
+1

非常感谢这个例子。我会看看我能否理解这一切。我会好好利用这个。 – xX4m4zingXx

+0

*“我会看看我是否可以理解它。”*重要的部分在'initUI()'方法中,甚至可以缩短删除重写方法以放大进度条大小的方法。 –

+1

啊我明白了。非常感谢你帮助我。 – xX4m4zingXx