出于某种原因,“Java Text”JLabel并不居中。我已经查明了如何做到这一点并看到了各种各样的例子,最有希望的是http://www.java2s.com/Code/Java/Swing-JFC/AsimpledemonstrationoftextalignmentinJLabels.htm,但它不起作用。如果您希望自己使用一些代码内注释来运行它,那么这是完整的代码,因此您不必费心搜索整个代码:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingConstants;
public class FontViewer
{
static JCheckBox checkBoxBold;
static JCheckBox checkBoxItalic;
static JCheckBox checkBoxCenter;
static JPanel textPanel;
static JLabel textLabel;
static JComboBox fontName;
static JComboBox fontSize;
static JRadioButton redButton;
static JRadioButton whiteButton;
static JRadioButton blueButton;
static ActionListener listener;
public static void main(String[] args)
{
final int FRAME_SIZE_X = 250;
final int FRAME_SIZE_Y = 400;
JFrame frame = new JFrame();
frame.setSize(FRAME_SIZE_X, FRAME_SIZE_Y);
JPanel face = new JPanel();
face.setLayout(new GridLayout(2, 1));
// listener inner class
class FontListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
int fontStyle = 0;
if (checkBoxBold.isSelected())
fontStyle = fontStyle + Font.BOLD;
if (checkBoxItalic.isSelected())
fontStyle = fontStyle + Font.ITALIC;
// this if statement does not work
if (checkBoxCenter.isSelected())
textLabel.setHorizontalAlignment(SwingConstants.CENTER);
String textFont = (String) fontName.getSelectedItem();
int textSize = Integer.parseInt((String) fontSize.getSelectedItem());
textLabel.setFont(new Font(textFont, fontStyle, textSize));
if (redButton.isSelected())
textLabel.setForeground(Color.RED);
else if (whiteButton.isSelected())
textLabel.setForeground(Color.WHITE);
else if (blueButton.isSelected())
textLabel.setForeground(Color.BLUE);
textLabel.repaint();
}
}
listener = new FontListener();
JPanel bottomFace = new JPanel();
bottomFace.setLayout(new GridLayout(3, 1));
textPanel = createTextPanel();
JPanel checkBoxPanel = createCheckBoxPanel();
JPanel comboPanel = createComboPanel();
JPanel radioButtonsPanel = createButtonsPanel();
face.add(textPanel);
bottomFace.add(checkBoxPanel);
bottomFace.add(comboPanel);
bottomFace.add(radioButtonsPanel);
face.add(bottomFace);
frame.add(face);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private static JPanel createTextPanel()
{
final int DEFAULT_FONT_SIZE = 12;
textPanel = new JPanel();
textPanel.setLayout(new BorderLayout());
textLabel = new JLabel("Java Text");
textLabel.setFont(new Font("Times", 0, DEFAULT_FONT_SIZE));
textPanel.add(textLabel, BorderLayout.WEST);
return textPanel;
}
// check boxes created and programmed here
private static JPanel createCheckBoxPanel()
{
JPanel checkBoxPanel = new JPanel();
checkBoxBold = new JCheckBox("Bold");
checkBoxItalic = new JCheckBox("Italic");
checkBoxCenter = new JCheckBox("Center");
checkBoxBold.addActionListener(listener);
checkBoxItalic.addActionListener(listener);
checkBoxCenter.addActionListener(listener);
checkBoxPanel.add(checkBoxBold);
checkBoxPanel.add(checkBoxItalic);
checkBoxPanel.add(checkBoxCenter);
return checkBoxPanel;
}
private static JPanel createComboPanel()
{
JPanel comboPanel = new JPanel();
fontName = new JComboBox();
fontName.addItem("Times");
fontName.addItem("Serif");
fontName.addItem("Courier");
fontSize = new JComboBox();
fontSize.addItem("12");
fontSize.addItem("24");
fontSize.addItem("36");
comboPanel.add(fontName);
comboPanel.add(fontSize);
fontName.addActionListener(listener);
fontSize.addActionListener(listener);
return comboPanel;
}
private static JPanel createButtonsPanel()
{
JPanel radioButtonsPanel = new JPanel();
redButton = new JRadioButton("Red");
whiteButton = new JRadioButton("White");
blueButton = new JRadioButton("Blue");
redButton.addActionListener(listener);
whiteButton.addActionListener(listener);
blueButton.addActionListener(listener);
ButtonGroup colors = new ButtonGroup();
colors.add(redButton);
colors.add(whiteButton);
colors.add(blueButton);
radioButtonsPanel.add(redButton);
radioButtonsPanel.add(whiteButton);
radioButtonsPanel.add(blueButton);
return radioButtonsPanel;
}
}
我对这种异常感到困惑,非常感谢任何帮助或建议!非常感谢你。
答案 0 :(得分:6)
textLabel
已锚定在BorderLayout.WEST
的{{1}}位置。将其移至中心位置:
textPanel