我正在用Java編寫一個具有UI的程序。我想製作一種健康吧的東西。我必須JLabel
s HealthBarUnder和HealthBarOver。我想將它們放在彼此的頂部,這樣我就可以減少HealthBarOver的寬度(從而使健康欄的外觀)。什麼是最好的佈局使用。我正在使用BorderLayout
,但它不會讓我重新調整組件大小。Java堆棧組件
謝謝
我正在用Java編寫一個具有UI的程序。我想製作一種健康吧的東西。我必須JLabel
s HealthBarUnder和HealthBarOver。我想將它們放在彼此的頂部,這樣我就可以減少HealthBarOver的寬度(從而使健康欄的外觀)。什麼是最好的佈局使用。我正在使用BorderLayout
,但它不會讓我重新調整組件大小。Java堆棧組件
謝謝
我會用這個小方法,以及給出更手動方法:
private void addComponent(Container container, Component c, int x, int y,int width, int height) {
c.setBounds(x, y, width, height);
container.add(c);
}
,並調用它是這樣的:
addComponent(container such as JPanel, component such as a JButton, x position, yposition, width, height);
BoxLayout如何疊加組件? – MadProgrammer 2013-02-28 00:49:17
愚蠢的答案我給了,對不起, – DOWmad 2013-02-28 00:54:16
別擔心,它嚇壞了我:P – MadProgrammer 2013-02-28 00:56:59
你「能」做像這樣...
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;
public class SlidingLabels {
public static void main(String[] args) {
new SlidingLabels();
}
public SlidingLabels() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JLabel lower = new JLabel();
private JLabel upper = new JLabel();
private float progress = 1f;
private boolean ignoreUpdates;
public TestPane() {
setLayout(new GridBagLayout());
lower.setOpaque(true);
lower.setBackground(Color.GRAY);
lower.setBorder(new LineBorder(Color.BLACK));
lower.setPreferredSize(new Dimension(200, 25));
upper.setOpaque(true);
upper.setBackground(Color.BLUE);
upper.setBorder(new LineBorder(Color.BLACK));
upper.setPreferredSize(new Dimension(200, 25));
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.NONE;
add(upper, gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
add(lower, gbc);
Timer timer = new Timer(500, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
progress -= 0.01;
if (progress <= 0.001) {
((Timer)e.getSource()).stop();
}
updateProgress();
}
});
timer.setRepeats(true);
timer.setCoalesce(true);
timer.start();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
protected void updateProgress() {
ignoreUpdates = true;
int width = (int) (getWidth() * progress);
upper.setPreferredSize(new Dimension(width, 25));
revalidate();
repaint();
ignoreUpdates = false;
}
@Override
public void invalidate() {
super.invalidate();
if (!ignoreUpdates) {
updateProgress();
}
}
}
}
,但它使用了一些討厭的黑客,並可能會在你面對提前而後來炸燬....
您應該使用JProgressBar
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;
public class ProgressBar {
public static void main(String[] args) {
new ProgressBar();
}
public ProgressBar() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JProgressBar pb;
private float progress = 1f;
public TestPane() {
setLayout(new GridBagLayout());
pb = new JProgressBar();
pb.setBorderPainted(false);
pb.setStringPainted(true);
pb.setBorder(new LineBorder(Color.BLACK));
pb.setForeground(Color.BLUE);
pb.setBackground(Color.GRAY);
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1;
gbc.insets = new Insets(4, 4, 4, 4);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
add(pb, gbc);
updateProgress();
Timer timer = new Timer(500, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
progress -= 0.01;
if (progress <= 0.001) {
((Timer)e.getSource()).stop();
}
updateProgress();
}
});
timer.setRepeats(true);
timer.setCoalesce(true);
timer.start();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
protected void updateProgress() {
pb.setValue((int) (100 * progress));
}
}
}
但是,如果這不符合您的需求,您會更好地編寫自己的進度組件...
public class ProgressPane {
public static void main(String[] args) {
new ProgressPane();
}
public ProgressPane() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private float progress = 1f;
public TestPane() {
setOpaque(false);
setForeground(Color.BLUE);
setBackground(Color.GRAY);
Timer timer = new Timer(500, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
progress -= 0.01;
if (progress <= 0.001) {
((Timer)e.getSource()).stop();
}
repaint();
}
});
timer.setRepeats(true);
timer.setCoalesce(true);
timer.start();
}
@Override
public Dimension getPreferredSize() {
FontMetrics fm = getFontMetrics(getFont());
return new Dimension(200, fm.getHeight() + 4);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int width = getWidth() - 4;
int height = getHeight() - 4;
int x = 2;
int y = 2;
g.setColor(getBackground());
g.fillRect(x, y, width, height);
g.setColor(Color.BLACK);
g.drawRect(x, y, width, height);
g.setColor(getForeground());
g.fillRect(x, y, (int) (width * progress), height);
g.setColor(Color.BLACK);
g.drawRect(x, y, (int) (width * progress), height);
FontMetrics fm = g.getFontMetrics();
String value = NumberFormat.getPercentInstance().format(progress);
x = x + ((width - fm.stringWidth(value))/2);
y = y + ((height - fm.getHeight())/2);
g.setColor(Color.WHITE);
g.drawString(value, x, y + fm.getAscent());
}
}
}
我會HIGHLY建議的最後兩個例子中的一個,他們是更簡單的實現和維護隨着時間的推移。第一個會爆炸,很不好過,在你的臉上
PS- Kleo,請不要傷害我:(
好吧,那麼你將如何設置進度條的圖像,而不是普通的顏色... – user1976336 2013-02-28 23:02:51
我可能會使用第三個例子這種情況下......否則你需要爲'JProgressBar'編寫一個新的UI實現,但這可能會有效t應用程序中的所有'JProgressBar's – MadProgrammer 2013-02-28 23:12:03
'GridBagLayout'可以做到這一點,但它可能是eaiser簡單地使用['JProgressBar' ](http://docs.oracle.com/javase/tutorial/uiswing/components/progress.html) – MadProgrammer 2013-02-28 00:43:59
CardLayout?........... – 2013-02-28 00:50:25