【发布时间】:2015-04-13 20:23:55
【问题描述】:
/ 我的目标是将侦听器对象制作到此GUI,以便在选择的字体样式或字体系列中的更改时,或按下“确定”按钮,然后字体的全名是显示在文本字段中。提前谢谢你。/
import java.awt.*;
import java.awt.event.*;
import java.awt.Component.*;
import javax.swing.*;
public class QuestionTwo {
public static void main(String[] args) {
CheckRadio two = new CheckRadio("Font Chooser");
two.init();
}
}
@SuppressWarnings("serial")
class CheckRadio extends JFrame {
public CheckRadio(String s) {
super(s);
}
public void init() {
JPanel check = new JPanel();
check.setLayout(new GridLayout(2, 1));
check.add(new JCheckBox("Bold"));
check.setFont(getFont().deriveFont(Font.BOLD)); //getFont() method can be found through out the code`enter code here. Bottom down at the Listener method I also did create a getFont() method for those listener.
check.add(new JCheckBox("Italic"));
check.setFont(getFont().deriveFont(Font.ITALIC));
JPanel radio = new JPanel();
radio.setLayout(new GridLayout(3, 1));
ButtonGroup group = new ButtonGroup();
JRadioButton ti = new JRadioButton("Times");
JRadioButton he = new JRadioButton("Heltivica");
JRadioButton co = new JRadioButton("Courier");
ti.setFont(getFont().deriveFont(Font.TRUETYPE_FONT,15));
he.setFont(getFont().deriveFont(Font.TRUETYPE_FONT,16));
co.setFont(getFont().deriveFont(Font.TRUETYPE_FONT,17));
group.add(ti);
group.add(he);
group.add(co);
radio.add(ti);
radio.add(he);
radio.add(co);
JLabel textBox = new JLabel();
textBox.setLayout(new GridLayout(3, 1));
textBox.add(new JLabel(""));
textBox.add(new JTextField(10));
textBox.add(new JLabel(""));
JLabel okButton = new JLabel();
okButton.setLayout(new GridLayout(3, 1));
okButton.add(new JLabel(""));
okButton.add(new JButton("OK"));
okButton.add(new JLabel(""));
Container panel = this.getContentPane();
panel.setLayout(new GridLayout(1, 4));
panel.add(check);
panel.add(radio);
panel.add(textBox);
panel.add(okButton);
ti.addActionListener(new TimesListener(textBox));
he.addActionListener(new HelvticaListener(textBox));
co.addActionListener(new CourierListener(textBox));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setVisible(true);
}
}
class TimesListener implements ActionListener {
// JLabel okButton;
JButton okButton;
JRadioButton ti;
JCheckBox bo, it;
JLabel textBox;
Font f;
public TimesListener(JLabel textBox) {
this.textBox = textBox;
}
public TimesListener(JButton okButton, JRadioButton ti, JCheckBox bo,
JCheckBox it, JLabel textBox) {
this.okButton = okButton;
this.ti = ti;
this.bo = bo;
this.it = it;
this.textBox = textBox;
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == okButton) {
if (ti.isSelected()) {
if (bo.isSelected()) {
if (it.isSelected()) {
textBox.setFont(getFont().deriveFont(
Font.TRUETYPE_FONT + Font.BOLD + Font.ITALIC,
15));
textBox.setText("Times");
} else {
textBox.setFont(getFont().deriveFont(
Font.TRUETYPE_FONT + Font.BOLD, 15));
textBox.setText("Times");
}
} else if (it.isSelected()) {
textBox.setFont(getFont().deriveFont(
Font.TRUETYPE_FONT + Font.ITALIC, 15));
textBox.setText("Times");
} else {
textBox.setFont(getFont()
.deriveFont(Font.TRUETYPE_FONT, 15));
textBox.setText("Times");
}
}
}
}
private Font getFont() {
return null;
}
}
class HelvticaListener implements ActionListener {
JButton okButton;
JRadioButton he;
JCheckBox bo, it;
JLabel textBox;
Font f;
public HelvticaListener(JLabel textBox) {
this.textBox = textBox;
}
public HelvticaListener(JButton okButton, JRadioButton he, JCheckBox bo,
JCheckBox it, JLabel textBox) {
this.okButton = okButton;
this.he = he;
this.bo = bo;
this.it = it;
this.textBox = textBox;
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == okButton) {
if (he.isSelected()) {
if (bo.isSelected()) {
if (it.isSelected()) {
textBox.setFont(getFont().deriveFont(
Font.TRUETYPE_FONT + Font.BOLD + Font.ITALIC,
16));
textBox.setText("Helvtica");
} else {
textBox.setFont(getFont().deriveFont(
Font.TRUETYPE_FONT + Font.BOLD, 16));
textBox.setText("Helvtica");
}
} else if (it.isSelected()) {
textBox.setFont(getFont().deriveFont(
Font.TRUETYPE_FONT + Font.ITALIC, 16));
textBox.setText("Helvtica");
} else {
textBox.setFont(getFont()
.deriveFont(Font.TRUETYPE_FONT, 16));
textBox.setText("Helvtica");
}
}
}
}
private Font getFont() {
return null;
}
}
class CourierListener implements ActionListener {
// JLabel okButton;
JButton okButton;
JRadioButton co;
JCheckBox bo, it;
JLabel textBox;
Font f;
public CourierListener(JLabel textBox) {
this.textBox = textBox;
}
public CourierListener(JButton okButton, JRadioButton co, JCheckBox bo,
JCheckBox it, JLabel textBox) {
this.okButton = okButton;
this.co = co;
this.bo = bo;
this.it = it;
this.textBox = textBox;
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == okButton) {
if (co.isSelected()) {
if (bo.isSelected()) {
if (it.isSelected()) {
textBox.setFont(getFont().deriveFont(
Font.TRUETYPE_FONT + Font.BOLD + Font.ITALIC,
18));
textBox.setText("Courier");
} else {
textBox.setFont(getFont().deriveFont(
Font.TRUETYPE_FONT + Font.BOLD, 18));
textBox.setText("Courier");
}
} else if (it.isSelected()) {
textBox.setFont(getFont().deriveFont(
Font.TRUETYPE_FONT + Font.ITALIC, 18));
textBox.setText("Courier");
} else {
textBox.setFont(getFont()
.deriveFont(Font.TRUETYPE_FONT, 18));
textBox.setText("Courier");
}
}
}
}
private Font getFont() {
return null;
}
}
【问题讨论】:
-
认真的吗?
getFont()方法返回 null。所以当然取消引用它会抛出一个 NullPointerException。 -
我是初学者,谁能帮我解决我的问题,getFont()方法该怎么办?谢谢
-
哪一行代码抛出NullPointerException(或简称NPE)?
-
private Font getFont() { return null; }对您来说似乎没有问题? -
如果您有类似的问题供将来参考,
"Line 31"告诉我们很少使用,因为我们都不想在您发布的代码中逐行计算每一行,希望您已发布 所有代码包括包声明并且没有跳过任何行。您需要发布实际线路,以便我们搜索它。
标签: java swing user-interface fonts awt