我创建了一个基本的GUI,它有一个按钮组的字体样式(时间,helvetica,courier)中有2个JCheckButtons(粗体,斜体)和3JRadioButtons。还有一个文本字段,它将在该样式中显示字体样式的字符串,例如时间,如果按下粗体或斜体按钮,则文本也将是粗体或斜体(或两者)。到目前为止,我已设法添加动作侦听器,将字体名称从单选按钮复制到文本字段,但我无法将此文本设置为关联的字体样式。我也无法使用粗体和斜体按钮的功能。
这是我的GUI布局的代码(布局是我想要的,只有我的按钮的功能问题:
$_GET['id']
以下是我的动作听众的课程:
package weekTwo;
import javax.swing.*;
import java.awt.*;
import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class taskTwo {
public static void main(String[] args) {
JFrame window = new JFrame("Font Chooser");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(500, 100);
FontSetter fontSetter = new FontSetter();
Container pane = window.getContentPane();
pane.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
JTextField text = new JTextField();
gbc.gridx = 2;
gbc.gridy = 2;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.insets = new Insets(0, 0, 5, 5); //padding constraints
gbc.fill = GridBagConstraints.BOTH; //text field fills cell
pane.add(text, gbc);
JCheckBox bold = new JCheckBox("Bold");
gbc.gridx = 0; //sets x position to 0
gbc.gridy = 1;
bold.addActionListener(new Bold(fontSetter, text));
pane.add(bold, gbc);
JCheckBox italic = new JCheckBox("Italic");
gbc.gridx = 0;
gbc.gridy = 3;
italic.addActionListener(new Italic(fontSetter, text));
pane.add(italic, gbc);
JRadioButton times = new JRadioButton("Times", true);
gbc.gridx = 1;
gbc.gridy = 0;
times.addActionListener(new Times(fontSetter, text));
pane.add(times, gbc);
JRadioButton helvetica = new JRadioButton("Helvetica", false);
gbc.gridx = 1;
gbc.gridy = 2;
helvetica.addActionListener(new Helvetica(fontSetter, text));
pane.add(helvetica, gbc);
JRadioButton courier = new JRadioButton("Courier", false);
gbc.gridx = 1;
gbc.gridy = 4;
courier.addActionListener(new Courier(fontSetter, text));
pane.add(courier, gbc);
ButtonGroup fonts = new ButtonGroup(); //now only one of these buttons can be selected at once
fonts.add(times);
fonts.add(helvetica);
fonts.add(courier);
JButton ok = new JButton("OK");
gbc.gridx = 3;
gbc.gridy = 2;
//ok.addActionListener();
pane.add(ok, gbc);
window.setVisible(true);
}
}
我希望我已经清楚地解释了这一点,并希望对我的代码有任何帮助,谢谢。
p.s也是我的fontsetter类,它没有任何函数atm,但它是我最初打算布置我的动作监听器的方式
class Bold implements ActionListener {
private final FontSetter fontSetter;
private final JTextField textfield;
Bold(FontSetter fontSetter, JTextField textfield) {
this.fontSetter = fontSetter;
this.textfield = textfield;
}
Font font;
public void actionPerformed(ActionEvent e) {
textfield.setFont(font.deriveFont(Font.BOLD));
}
}
class Italic implements ActionListener {
private final FontSetter fontSetter;
private final JTextField textfield;
Italic(FontSetter fontSetter, JTextField textfield) {
this.fontSetter = fontSetter;
this.textfield = textfield;
}
public void actionPerformed(ActionEvent e) {
textfield.setText("");
}
}
class BoldItalic implements ActionListener {
private final FontSetter fontSetter;
private final JTextField textfield;
BoldItalic(FontSetter fontSetter, JTextField textfield) {
this.fontSetter = fontSetter;
this.textfield = textfield;
}
public void actionPerformed(ActionEvent e) {
textfield.setText("");
}
}
class Times implements ActionListener {
private final FontSetter fontSetter;
private final JTextField textfield;
Times(FontSetter fontSetter, JTextField textfield) {
this.fontSetter = fontSetter;
this.textfield = textfield;
}
public void actionPerformed(ActionEvent e) {
textfield.setText("Times");
}
}
class Helvetica implements ActionListener {
private final FontSetter fontSetter;
private final JTextField textfield;
Helvetica(FontSetter fontSetter, JTextField textfield) {
this.fontSetter = fontSetter;
this.textfield = textfield;
}
public void actionPerformed(ActionEvent e) {
textfield.setText("Helvetica");
}
}
class Courier implements ActionListener {
private final FontSetter fontSetter;
private final JTextField textfield;
Courier(FontSetter fontSetter, JTextField textfield) {
this.fontSetter = fontSetter;
this.textfield = textfield;
}
public void actionPerformed(ActionEvent e) {
textfield.setText("Courier");
}
}
答案 0 :(得分:2)
您可以在FontSetter
类中定义一些变量以保留当前的字体名称和样式
像这样的东西
class FontSetter {
private final JTextField text;
private String fontName = "Times";
private boolean isBold = false;
private boolean isItalic = false;
FontSetter(JTextField text) {
this.text = text;
}
void setBold(boolean flag) {
isBold = flag;
applyFontChanges();
}
void setItalic(boolean flag) {
isItalic = flag;
applyFontChanges();
}
void setFontName(String fontName) {
this.fontName = fontName;
applyFontChanges();
}
private void applyFontChanges(){
Font font;
int style;
if(isBold && isItalic){
style = Font.BOLD + Font.ITALIC;
} else if(isBold){
style = Font.BOLD;
} else if(isItalic){
style = Font.ITALIC;
} else {
style = Font.PLAIN;
}
font = new Font(fontName, style, 12);
text.setFont(font );
}
}
然后你可以在你的ActionListeners中使用它来改变像这样的字体
fontSetter.setFontName("Courier");
textfield.setText("Courier");
您还可以按照以下方式更改样式
JCheckBox ch = (JCheckBox) e.getSource();
fontSetter.setBold(ch.isSelected());
-