2010-06-14 39 views

回答

97

使用BorderLayout創建JFrame或JPanel,給它類似BevelBorder或行邊框,以便與其餘內容分離,然後在BorderLayout.SOUTH中添加狀態面板。

JFrame frame = new JFrame(); 
frame.setLayout(new BorderLayout()); 
frame.setSize(200, 200); 

// create the status bar panel and shove it down the bottom of the frame 
JPanel statusPanel = new JPanel(); 
statusPanel.setBorder(new BevelBorder(BevelBorder.LOWERED)); 
frame.add(statusPanel, BorderLayout.SOUTH); 
statusPanel.setPreferredSize(new Dimension(frame.getWidth(), 16)); 
statusPanel.setLayout(new BoxLayout(statusPanel, BoxLayout.X_AXIS)); 
JLabel statusLabel = new JLabel("status"); 
statusLabel.setHorizontalAlignment(SwingConstants.LEFT); 
statusPanel.add(statusLabel); 

frame.setVisible(true); 

這裏是我的機器上面的狀態欄代碼的結果:

enter image description here

+16

感謝上帝,這是關於唯一有用的java狀態條碼在整個互聯網上 – 2012-09-22 10:27:29

+1

沒有爲我工作,狀態欄結束了在中間的窗口 – 2015-12-30 22:08:58

+0

您需要在JPanel中使用不同的佈局。如上面的代碼片段所示,BoxLayout將產生像圖片中的結果。 – 2017-03-20 10:39:44

6

不幸的是搖擺不具​​有狀態條原生支持。 您可以使用BorderLayout和標籤或任何你需要在底部顯示:

public class StatusBar extends JLabel { 

    /** Creates a new instance of StatusBar */ 
    public StatusBar() { 
     super(); 
     super.setPreferredSize(new Dimension(100, 16)); 
     setMessage("Ready"); 
    } 

    public void setMessage(String message) { 
     setText(" "+message);   
    }   
} 

然後你的主面板:

statusBar = new StatusBar(); 
getContentPane().add(statusBar, java.awt.BorderLayout.SOUTH); 

來源:http://www.java-tips.org/java-se-tips/javax.swing/creating-a-status-bar.html

+0

使用JPanel,而不是JLabel的狀態欄是更好的主意。 – 2010-06-14 08:53:39

+0

@Bozhidar Batsov:是的。 – Simon 2010-06-14 10:36:22

2

我會推薦狀態欄組件在SwingX library - 這裏是API文檔status bar is here

A good example of its use is here.

玩得開心。

+4

在整個SwingX中實現一個微不足道的組件對我來說似乎不是最好的主意......如果你已經在應用程序中使用SwingX,那很好,但否則它是一個完整的矯枉過正...... – 2010-06-14 10:10:19

4

我已經使用了L2FProd的擺動庫。他們提供的狀態欄庫非常好。

下面是你將如何使用它:

  1. 下載JAR他們提供,並把它放在你的classpath
  2. 的狀態欄分爲內部酒吧區到區。每個區域可以包含一個組件(JLabel,JButton等)。想法是用所需區域和組件填滿欄。

  3. 實例化下面狀態欄....

    import java.awt.Component; 
    import javax.swing.BorderFactory; 
    import javax.swing.JLabel; 
    import com.l2fprod.common.swing.StatusBar; 
    
    StatusBar statusBar = new StatusBar(); 
    statusBar.setZoneBorder(BorderFactory.createLineBorder(Color.GRAY)); 
    statusBar.setZones(
        new String[] { "first_zone", "second_zone", "remaining_zones" }, 
        new Component[] { 
         new JLabel("first"), 
         new JLabel("second"), 
         new JLabel("remaining") 
        }, 
        new String[] {"25%", "25%", "*"} 
    ); 
    
  4. 現在添加上述statusBar到你的主面板(的BorderLayout並將其設置爲南側)。

查看我正在從事的其中一個應用here(它有2個區域)的樣本截圖。讓我知道如果你面臨任何問題....

+2

屏幕截圖是一個死鏈接。 – GKFX 2014-04-07 15:09:07

相關問題