状态栏怎么实现? 找了很长时间了,没看到java里有 状态栏 这样的组件啊!自己用JPanel和JTextField写吧,感觉效果不理想。大大们是怎么实现你们的状态栏的啊? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.*;import javax.swing.border.Border;public class MyStatusBar extends JPanel{ private static Border COLUMN_PANE_BORDER = BorderFactory.createCompoundBorder( BorderFactory.createCompoundBorder( BorderFactory.createMatteBorder(0, 0, 1, 1, SystemColor.controlLtHighlight), BorderFactory.createMatteBorder(1, 1, 0, 0, SystemColor.controlShadow)), BorderFactory.createEmptyBorder(0, 1, 0, 1)); private static Border COLUMN_PANE_BORDER_XP = BorderFactory.createCompoundBorder( BorderFactory.createCompoundBorder( BorderFactory.createMatteBorder(0, 0, 0, 1, SystemColor.controlLtHighlight), BorderFactory.createMatteBorder(0, 0, 0, 1, SystemColor.controlShadow)), BorderFactory.createEmptyBorder(0, 1, 0, 1)); public MyStatusBar(int columnCount, Component[] columnComps, int[] columnMinWidths, double[] columnResizeWeights) { super(new GridBagLayout()); String osName = System.getProperty("os.name"); boolean isXP = osName.toLowerCase().indexOf("xp") != -1; GridBagConstraints gbc = new GridBagConstraints(); gbc.fill = GridBagConstraints.BOTH; gbc.weighty = 1; for (int i = 0; i < columnCount; i++) { final int minWid = columnMinWidths[i]; JPanel columnPanel = new JPanel(new BorderLayout()) { public Dimension getPreferredSize() { Dimension d = super.getPreferredSize(); d.width = Math.max(minWid, d.width); return d; } }; if (!isXP) { columnPanel.setBorder(COLUMN_PANE_BORDER); } else { if (i == columnCount - 1) { columnPanel.setBorder(BorderFactory.createEmptyBorder()); } else { columnPanel.setBorder(COLUMN_PANE_BORDER_XP); } } gbc.weightx = columnResizeWeights[i]; if (i < columnCount - 1) { gbc.insets = new Insets(0, 0, 0, 2); } else { gbc.insets = new Insets(0, 0, 0, 0); } add(columnPanel, gbc); if (columnComps == null) { columnPanel.add(new JLabel(" ", JLabel.LEFT), BorderLayout.CENTER); } else { columnPanel.add(columnComps[i], BorderLayout.CENTER); } } } public Component getComponentAtColumn(int column) { JPanel columnPanel = (JPanel) getComponent(column); return columnPanel.getComponent(0); } public void setComponentAtColumn(int column, Component c) { JPanel columnPanel = (JPanel) getComponent(column); columnPanel.removeAll(); columnPanel.add(c, BorderLayout.CENTER); revalidate(); repaint(); } public static void main(String[] args) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) { e.printStackTrace(); } Component[] comps = new Component[] { new JLabel("5个项目", JLabel.LEFT), new JLabel("765字节", JLabel.LEFT), new JLabel("我的电脑", new ImageIcon(MyStatusBar.class.getResource("/imgs/mycomputer.png")), JLabel.LEFT)}; final MyStatusBar statusBar = new MyStatusBar( 3, comps, new int[] {100, 100, 150}, new double[] {1, 0, 0}); final JButton btn = new JButton("Start"); JPanel p = new JPanel(); p.add(btn); btn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { final JProgressBar progress = new JProgressBar(); progress.setBorder(BorderFactory.createEmptyBorder()); progress.setStringPainted(true); final Component oldComp = statusBar.getComponentAtColumn(0); statusBar.setComponentAtColumn(0, progress); btn.setEnabled(false); new Thread() { public void run() { for (int i = 0; i <= 100; i++) { final int value = i; SwingUtilities.invokeLater(new Runnable() { public void run() { progress.getModel().setValue(value); } }); try { Thread.sleep(20); } catch (InterruptedException ex) { ex.printStackTrace(); } } SwingUtilities.invokeLater(new Runnable() { public void run() { statusBar.setComponentAtColumn(0, oldComp); btn.setEnabled(true); } }); } }.start(); } }); JFrame f = new JFrame("StatusBarTest"); f.getContentPane().setLayout(new BorderLayout(0, 2)); p.setBorder(COLUMN_PANE_BORDER); f.getContentPane().add(p, BorderLayout.CENTER); f.getContentPane().add(statusBar, BorderLayout.SOUTH); f.setSize(500, 300); f.setLocationRelativeTo(null); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); }} GJApp.getStatusArea() static方法可以获得对状态栏的一个引用 GJApp是哪里的东西啊?swing中没有吧。我是想用swing实现。 用JPanel,把它的位置设计为Bottom,然后再设置它的边框setBorder() public class MyStatusBar extends Box { public JLabel lblObject,lblSize,lblDesc; public MyStatusBar() { super(BoxLayout.X_AXIS); Toolkit kit = Toolkit.getDefaultToolkit(); Dimension screenSize = kit.getScreenSize(); //设置被选对象数 lblObject = new JLabel("Selected Object(s):",SwingConstants.LEADING); lblObject.setPreferredSize(new Dimension((int) (0.3 * screenSize.width),22)); lblObject.setBorder(BorderFactory.createLoweredBevelBorder()); this.add(lblObject, null); //文件大小 lblSize = new JLabel("Size:", SwingConstants.LEADING); lblSize.setPreferredSize(new Dimension((int) (0.3 * screenSize.width),22)); lblSize.setBorder(BorderFactory.createLoweredBevelBorder()); this.add(lblSize, null); //文件描述 lblDesc = new JLabel("Description: Resource Manager", SwingConstants.LEADING); lblDesc.setPreferredSize(new Dimension((int) (0.5 * screenSize.width),22)); lblDesc.setBorder(BorderFactory.createLoweredBevelBorder()); this.add(lblDesc, null); } 为什么在执行线程是重画圆时还是在一个位置? [求助]JDBC连接ORACLE92060(其他版本正常)报ORA-00942报错,可表是存在的,请高手指教, 如何在线程中改变UI 多线程问题,不明白结果 请教一个简单问题 虽然是个小问题,可是困扰了我很久 刚才去色情网站转了一下,一会功夫通过applet居然被自动安装了一个软件,问:不是applet很安全吗?这是如何实现的? 请高手一定回答:关于jbuilder的使用 Java中的空值用什么来表示? 各位大侠,weblogic websphere tomcat 各有什么特点,那个更好呢? 新手问JTextField问题 工作经验问题
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.*;
import javax.swing.border.Border;public class MyStatusBar extends JPanel
{
private static Border COLUMN_PANE_BORDER =
BorderFactory.createCompoundBorder(
BorderFactory.createCompoundBorder(
BorderFactory.createMatteBorder(0, 0, 1, 1, SystemColor.controlLtHighlight),
BorderFactory.createMatteBorder(1, 1, 0, 0, SystemColor.controlShadow)),
BorderFactory.createEmptyBorder(0, 1, 0, 1));
private static Border COLUMN_PANE_BORDER_XP =
BorderFactory.createCompoundBorder(
BorderFactory.createCompoundBorder(
BorderFactory.createMatteBorder(0, 0, 0, 1, SystemColor.controlLtHighlight),
BorderFactory.createMatteBorder(0, 0, 0, 1, SystemColor.controlShadow)),
BorderFactory.createEmptyBorder(0, 1, 0, 1));
public MyStatusBar(int columnCount, Component[] columnComps,
int[] columnMinWidths, double[] columnResizeWeights)
{
super(new GridBagLayout());
String osName = System.getProperty("os.name");
boolean isXP = osName.toLowerCase().indexOf("xp") != -1;
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.weighty = 1; for (int i = 0; i < columnCount; i++) {
final int minWid = columnMinWidths[i]; JPanel columnPanel = new JPanel(new BorderLayout()) {
public Dimension getPreferredSize()
{
Dimension d = super.getPreferredSize();
d.width = Math.max(minWid, d.width);
return d;
}
};
if (!isXP) {
columnPanel.setBorder(COLUMN_PANE_BORDER);
}
else {
if (i == columnCount - 1) {
columnPanel.setBorder(BorderFactory.createEmptyBorder());
}
else {
columnPanel.setBorder(COLUMN_PANE_BORDER_XP);
}
}
gbc.weightx = columnResizeWeights[i];
if (i < columnCount - 1) {
gbc.insets = new Insets(0, 0, 0, 2);
}
else {
gbc.insets = new Insets(0, 0, 0, 0);
}
add(columnPanel, gbc);
if (columnComps == null) {
columnPanel.add(new JLabel(" ", JLabel.LEFT), BorderLayout.CENTER);
}
else {
columnPanel.add(columnComps[i], BorderLayout.CENTER);
}
}
}
public Component getComponentAtColumn(int column)
{
JPanel columnPanel = (JPanel) getComponent(column);
return columnPanel.getComponent(0);
}
public void setComponentAtColumn(int column, Component c) {
JPanel columnPanel = (JPanel) getComponent(column);
columnPanel.removeAll();
columnPanel.add(c, BorderLayout.CENTER);
revalidate();
repaint();
} public static void main(String[] args)
{
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
} Component[] comps = new Component[] {
new JLabel("5个项目", JLabel.LEFT),
new JLabel("765字节", JLabel.LEFT),
new JLabel("我的电脑", new ImageIcon(MyStatusBar.class.getResource("/imgs/mycomputer.png")), JLabel.LEFT)};
final MyStatusBar statusBar = new MyStatusBar(
3, comps, new int[] {100, 100, 150}, new double[] {1, 0, 0});
final JButton btn = new JButton("Start");
JPanel p = new JPanel();
p.add(btn);
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
final JProgressBar progress = new JProgressBar();
progress.setBorder(BorderFactory.createEmptyBorder());
progress.setStringPainted(true); final Component oldComp = statusBar.getComponentAtColumn(0);
statusBar.setComponentAtColumn(0, progress);
btn.setEnabled(false);
new Thread() {
public void run() {
for (int i = 0; i <= 100; i++) {
final int value = i;
SwingUtilities.invokeLater(new Runnable() {
public void run() {
progress.getModel().setValue(value);
}
}); try {
Thread.sleep(20);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
SwingUtilities.invokeLater(new Runnable() {
public void run() {
statusBar.setComponentAtColumn(0, oldComp);
btn.setEnabled(true);
}
});
}
}.start();
}
});
JFrame f = new JFrame("StatusBarTest");
f.getContentPane().setLayout(new BorderLayout(0, 2));
p.setBorder(COLUMN_PANE_BORDER);
f.getContentPane().add(p, BorderLayout.CENTER);
f.getContentPane().add(statusBar, BorderLayout.SOUTH);
f.setSize(500, 300);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
我是想用swing实现。
public JLabel lblObject,lblSize,lblDesc;
public MyStatusBar() {
super(BoxLayout.X_AXIS);
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
//设置被选对象数
lblObject = new JLabel("Selected Object(s):",SwingConstants.LEADING);
lblObject.setPreferredSize(new Dimension((int) (0.3 * screenSize.width),22));
lblObject.setBorder(BorderFactory.createLoweredBevelBorder());
this.add(lblObject, null);
//文件大小
lblSize = new JLabel("Size:", SwingConstants.LEADING);
lblSize.setPreferredSize(new Dimension((int) (0.3 * screenSize.width),22));
lblSize.setBorder(BorderFactory.createLoweredBevelBorder());
this.add(lblSize, null);
//文件描述
lblDesc = new JLabel("Description: Resource Manager", SwingConstants.LEADING);
lblDesc.setPreferredSize(new Dimension((int) (0.5 * screenSize.width),22));
lblDesc.setBorder(BorderFactory.createLoweredBevelBorder());
this.add(lblDesc, null);
}