如何使用按钮 awt/close window?
How to use buttons awt/close window?
晚上好!
我有理解问题,如何使用按钮的实现(awt)
我怎样才能在每次点击时增加像答案一样的字体,而不仅仅是一次?
P.s。我怎样才能在我的框架 (300x300) 的边界中实现 textArea,并在南边放置 buttonPanel?
public class GUI extends Frame {
public GUI() throws HeadlessException {
Frame frame = new Frame();
frame.setVisible(true);
frame.setTitle("Test window");
frame.setSize(300, 300);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
//System.exit(0);
frame.dispose();
}
});
String fontName = "Arial";
int fontStyle = 10;
int fontSize = 12;
Font font = new Font(fontName, fontStyle, fontSize);
Panel mainPanel = new Panel();
Panel textPlacePanel = new Panel();
Panel buttonPlacePanel = new Panel();
Button increaseButton = new Button("Increase");
Button decreaseButton = new Button("Decrease");
Label label = new Label("Font size");
TextArea textArea = new TextArea();
textArea.setFont(font);
frame.add(mainPanel,BorderLayout.CENTER);
frame.add(buttonPlacePanel,BorderLayout.SOUTH);
frame.add(textPlacePanel,BorderLayout.CENTER);
buttonPlacePanel.add(label);
buttonPlacePanel.add(increaseButton);
buttonPlacePanel.add(decreaseButton);
textPlacePanel.add(textArea);
increaseButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int i = font.getSize();
Font font = new Font(fontName,fontStyle,++i);
textArea.setFont(font);
}
});
decreaseButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int i = font.getSize();
Font font = new Font(fontName,fontStyle,--i);
textArea.setFont(font);
}
});
}
public static void main(String[] args) {
GUI gui = new GUI();
}
}
How can i increase font like answer on every my click
那么,你的问题就在这里...
int i = font.getSize();
Font font = new Font(fontName, fontStyle, i);
您正在使用在构造函数中创建的 font
的实例,但随后您创建了 Font
的新本地实例(在 ActionListener
的上下文中)并且将其应用于 TextArea
,这意味着基本大小始终为 12。
相反,使用 Font#deriveFont
public class GUI extends Frame {
private Font font;
// Don't use hard coded values, use the constent names, its easier
// to read
private int fontStyle = Font.PLAIN;
private int fontSize = 12;
public GUI() {
String fontName = "Arial";
font = new Font(fontName, fontStyle, fontSize);
//...
increaseButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
font.deriveFont(++fontSize);
textArea.setFont(font);
}
});
//...
how can i realise textArea just in border of my frame(300x300) with a place for buttonPanel on south?
BorderLayout
将只管理添加到它管理的五个可用位置之一的最后一个组件。我不知道 mainPanel
在做什么,但它有点毫无意义,所以我会摆脱它。
此外,Panel
的默认布局是 Flowlayout
,不确定为什么要将文本区域合并为一个,但我会将布局管理器更改为更有用的东西
Font font = new Font(fontName, fontStyle, fontSize);
//Panel mainPanel = new Panel();
Panel textPlacePanel = new Panel(new BorderLayout());
Panel buttonPlacePanel = new Panel(new GridLayout(1, 3));
Button increaseButton = new Button("Increase");
Button decreaseButton = new Button("Decrease");
Label label = new Label("Font size");
TextArea textArea = new TextArea();
textArea.setFont(font);
//frame.add(mainPanel, BorderLayout.CENTER);
frame.add(buttonPlacePanel, BorderLayout.SOUTH);
frame.add(textPlacePanel, BorderLayout.CENTER);
我也会使用 pack
而不是 setSie
,但这可能需要一些额外的配置,而且由于 AWT 已经 17 年没有成为主流,我不在乎试着记住你可能需要做些什么来纠正。忠告,考虑改用 Swing 或 JavaFX,你会得到更好的支持
晚上好! 我有理解问题,如何使用按钮的实现(awt) 我怎样才能在每次点击时增加像答案一样的字体,而不仅仅是一次? P.s。我怎样才能在我的框架 (300x300) 的边界中实现 textArea,并在南边放置 buttonPanel?
public class GUI extends Frame {
public GUI() throws HeadlessException {
Frame frame = new Frame();
frame.setVisible(true);
frame.setTitle("Test window");
frame.setSize(300, 300);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
//System.exit(0);
frame.dispose();
}
});
String fontName = "Arial";
int fontStyle = 10;
int fontSize = 12;
Font font = new Font(fontName, fontStyle, fontSize);
Panel mainPanel = new Panel();
Panel textPlacePanel = new Panel();
Panel buttonPlacePanel = new Panel();
Button increaseButton = new Button("Increase");
Button decreaseButton = new Button("Decrease");
Label label = new Label("Font size");
TextArea textArea = new TextArea();
textArea.setFont(font);
frame.add(mainPanel,BorderLayout.CENTER);
frame.add(buttonPlacePanel,BorderLayout.SOUTH);
frame.add(textPlacePanel,BorderLayout.CENTER);
buttonPlacePanel.add(label);
buttonPlacePanel.add(increaseButton);
buttonPlacePanel.add(decreaseButton);
textPlacePanel.add(textArea);
increaseButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int i = font.getSize();
Font font = new Font(fontName,fontStyle,++i);
textArea.setFont(font);
}
});
decreaseButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int i = font.getSize();
Font font = new Font(fontName,fontStyle,--i);
textArea.setFont(font);
}
});
}
public static void main(String[] args) {
GUI gui = new GUI();
}
}
How can i increase font like answer on every my click
那么,你的问题就在这里...
int i = font.getSize();
Font font = new Font(fontName, fontStyle, i);
您正在使用在构造函数中创建的 font
的实例,但随后您创建了 Font
的新本地实例(在 ActionListener
的上下文中)并且将其应用于 TextArea
,这意味着基本大小始终为 12。
相反,使用 Font#deriveFont
public class GUI extends Frame {
private Font font;
// Don't use hard coded values, use the constent names, its easier
// to read
private int fontStyle = Font.PLAIN;
private int fontSize = 12;
public GUI() {
String fontName = "Arial";
font = new Font(fontName, fontStyle, fontSize);
//...
increaseButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
font.deriveFont(++fontSize);
textArea.setFont(font);
}
});
//...
how can i realise textArea just in border of my frame(300x300) with a place for buttonPanel on south?
BorderLayout
将只管理添加到它管理的五个可用位置之一的最后一个组件。我不知道 mainPanel
在做什么,但它有点毫无意义,所以我会摆脱它。
此外,Panel
的默认布局是 Flowlayout
,不确定为什么要将文本区域合并为一个,但我会将布局管理器更改为更有用的东西
Font font = new Font(fontName, fontStyle, fontSize);
//Panel mainPanel = new Panel();
Panel textPlacePanel = new Panel(new BorderLayout());
Panel buttonPlacePanel = new Panel(new GridLayout(1, 3));
Button increaseButton = new Button("Increase");
Button decreaseButton = new Button("Decrease");
Label label = new Label("Font size");
TextArea textArea = new TextArea();
textArea.setFont(font);
//frame.add(mainPanel, BorderLayout.CENTER);
frame.add(buttonPlacePanel, BorderLayout.SOUTH);
frame.add(textPlacePanel, BorderLayout.CENTER);
我也会使用 pack
而不是 setSie
,但这可能需要一些额外的配置,而且由于 AWT 已经 17 年没有成为主流,我不在乎试着记住你可能需要做些什么来纠正。忠告,考虑改用 Swing 或 JavaFX,你会得到更好的支持