我有一个JDialog,标题写在顶部。我将这个JDialog称为两种不同的情况,如果它不是默认情况,我将文本更改为其他内容。这种方法很好,但位置太远了。
我尝试了很多方法,例如:
TitleText.setText("Edit Fuse");
TitleText.setAlignmentY(JLabel.CENTER_ALIGNMENT);
//TitleText.setHorizontalAlignment(JDialog.);
//TitleText.setLayout(new FlowLayout(FlowLayout.LEFT));
他们都没有移动文本。我正在为整个JDialog使用自由设计布局。如果我必须创建另一个JLable并隐藏/取消隐藏,但我认为这很简单。有谁知道怎么做?
答案 0 :(得分:3)
我正在为整个JDialog使用免费设计版面
JLabel.CENTER_ALIGNMENT
,一个浮点数,而是使用JLabel.CENTER
,一个int,它是setHorizontalAlignment(...)方法的适当参数。例如:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
@SuppressWarnings("serial")
public class LayoutExample extends JPanel {
private static final float SIZE = 32;
private static final int TIMER_DELAY = 2000;
private String[] TITLE_STRINGS = { "Title 1", "Title 2",
"Some Random Title", "Fubars Rule!", "Snafus Drool!" };
private int titleIndex = 0;
private JLabel titleLabel = new JLabel(TITLE_STRINGS[titleIndex],
JLabel.CENTER);
public LayoutExample() {
titleLabel.setFont(titleLabel.getFont().deriveFont(Font.BOLD, SIZE));
setLayout(new BorderLayout());
add(titleLabel, BorderLayout.PAGE_START);
// the rest of your GUI could be added below
add(Box.createRigidArea(new Dimension(500, 300)), BorderLayout.CENTER);
new Timer(TIMER_DELAY, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
titleIndex++;
titleIndex %= TITLE_STRINGS.length;
titleLabel.setText(TITLE_STRINGS[titleIndex]);
}
}).start();
}
private static void createAndShowGui() {
JFrame frame = new JFrame("LayoutExample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new LayoutExample());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}