[英]How to call the Loop in Jtextfield
Im having trouble on calling my Loop that i created to be placed on the JTextField. Im only a beginner on GUI so i don't understand what i am missing or lacking . Please Help me.
the program must print a box of period if the user enters 1 and box of asterisk if the user
enters 2. and if the user enters 2 or more an error message will show up.
我重新编辑了代码先生。 这是我想出的问题,问题是我重新输入一个数字后,Jtextarea只是不断堆积打印件,它不刷新,我不知道为什么。例如,如果我输入1,则句点框将进行购物,如果我输入2,星号框出现在句点框的下面。它一直在堆积
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Box extends JFrame{
private JLabel numL,resultL;
private JTextField numTF;
private JTextArea resultTF;
private JButton printB,exitB;
private PrintButtonHandler pbHandler;
private ExitButtonHandler exitHandler;
public Box(){
numL=new JLabel("Enter 1 or 2", SwingConstants.CENTER);
resultL=new JLabel("Result",SwingConstants.CENTER);
numTF=new JTextField(20);
//resultTF=new JTextField(20);
resultTF = new JTextArea(5,5);
printB=new JButton("Print");
pbHandler=new PrintButtonHandler();
printB.addActionListener(pbHandler);
exitB=new JButton("Exit");
exitHandler= new ExitButtonHandler();
exitB.addActionListener(exitHandler);
setTitle("BOX");
Container p=getContentPane();
p.setLayout(new GridLayout(5,1));
p.add(numL);
p.add(numTF);
p.add(resultL);
p.add(resultTF);
p.add(printB);
p.add(exitB);
setSize(600,600);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private class PrintButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
//Box1 p=new Box1();
int num,height=5,width=5,numLL;
numLL=Integer.parseInt(numTF.getText());
Font f = resultTF.getFont();
resultTF.setFont(new Font(Font.MONOSPACED, f.getStyle(), f.getSize()));
if(numLL==1){
for(int i = 0; i < 5; i++) {
for(int j = 0; j < 5; j++) {
resultTF.append(".");
}
resultTF.append("\n");
}
}else if(numLL==2){
for(int i = 0; i < 5; i++) {
for(int j = 0; j < 5; j++) {
resultTF.append("*");
}
resultTF.append("\n");
}
}else if(numLL>2){
resultTF.append("NOT 1 OR 2:");
}
}
}
private class ExitButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
System.exit(0);
}
}
public static void main(String[]args){
Box p=new Box();
}
}
现在,您正在将盒子定向到System.out
。 因此,您需要将它们定向到您的文本组件。
另外,您不能为此使用JTextField
,因为它不是多行的。 相反,您应该在JScrollPane
使用类似JTextArea
东西。
resultTF = new JTextArea();
Font f = resultTF.getFont();
resultTF.setFont(new Font(Font.MONOSPACED, f.getStyle(), f.getSize()));
add(new JScrollPane(resultTF));
.
.
.
for(int i = 0; i < height; i++) {
for(int j = 0; j < width; j++) {
resultTF.append(".");
}
resultTF.append("\n");
}
如果您不希望使用滚动窗格,则还可以例如创建具有特定行和列的文本区域( new JTextArea(5, 5)
),使用StringBuilder
创建框并使用setText
而不是append
。
附带说明,您应该在Swing事件线程上创建GUI。 换句话说,您的main
应该包装在对invokeLater
的调用中:
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Box p = new Box();
}
});
}
也可以看看:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.