Java的swing组件无法显示
importjava.awt.*;importjavax.swing.*;importjava.awt.event.*;importjava.io.*;publiccla...
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
public class TextView extends JFrame implements ActionListener{
JButton file = new JButton("File");
JButton copy = new JButton("Copy");
JButton about = new JButton("About");
JButton exit = new JButton("Exit");
JLabel label = new JLabel("MY TEXT_SOFTWARE");
//label.setFont(new Font("Seriaf", Font.BOLD, 40));
TextArea text = new TextArea();
//Frame frame = new Frame();
Container frame = getContentPane();
Panel buttons = new Panel();
Panel labelShow = new Panel();
Panel textShow = new Panel();
public TextView(){
buttons.add(file);
buttons.add(copy);
buttons.add(about);
buttons.add(exit);
buttons.setLayout(new FlowLayout());
textShow.add(text);
textShow.setLayout(new FlowLayout());
labelShow.add(label);
frame.setLayout(new BorderLayout());
frame.add(labelShow, BorderLayout.CENTER);
frame.add(buttons, BorderLayout.NORTH);
frame.add(textShow, BorderLayout.SOUTH );
//frame.pack();
setVisible(true);
setSize(500, 500);
file.addActionListener(this);
exit.addActionListener(this);
about.addActionListener(this);
}
public void actionPerformed(ActionEvent ae){
/*Finish this function*/
if(ae.getSource() == exit)
System.exit(0);
/*Creat a FileDialog for user to choose one file*/
if(ae.getSource() == file){
FileDialog fd = new FileDialog(this, "Open File", FileDialog.LOAD);
fd.setVisible(true);
if(fd.getFile() != null){
File f= new File(fd.getDirectory() + fd.getFile());
if(f.exists())
readFile(f.toString());
else
text.setText("The file does not exits");
}
fd.dispose();
}
/*Show some information about this function*/
}
/*Read file if you select one*/
public void readFile(String file){
text.setText("");
try{
BufferedReader read = new BufferedReader(new FileReader(file));
String line;
//line = read.readLine();
while((line = read.readLine()) != null)
text.append(line + "\n");
read.close();
} catch (IOException ioe) {
System.err.println(ioe);
}
}
public static void main(String args[]){
TextView t = new TextView();
}
}
这个程序运行正常,但把JTextArea换成TextArea就无法显示,另一个与此类此的程序中把Label换成JLabel也无法显示,求各位大虾解释一下 谢谢啊 展开
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
public class TextView extends JFrame implements ActionListener{
JButton file = new JButton("File");
JButton copy = new JButton("Copy");
JButton about = new JButton("About");
JButton exit = new JButton("Exit");
JLabel label = new JLabel("MY TEXT_SOFTWARE");
//label.setFont(new Font("Seriaf", Font.BOLD, 40));
TextArea text = new TextArea();
//Frame frame = new Frame();
Container frame = getContentPane();
Panel buttons = new Panel();
Panel labelShow = new Panel();
Panel textShow = new Panel();
public TextView(){
buttons.add(file);
buttons.add(copy);
buttons.add(about);
buttons.add(exit);
buttons.setLayout(new FlowLayout());
textShow.add(text);
textShow.setLayout(new FlowLayout());
labelShow.add(label);
frame.setLayout(new BorderLayout());
frame.add(labelShow, BorderLayout.CENTER);
frame.add(buttons, BorderLayout.NORTH);
frame.add(textShow, BorderLayout.SOUTH );
//frame.pack();
setVisible(true);
setSize(500, 500);
file.addActionListener(this);
exit.addActionListener(this);
about.addActionListener(this);
}
public void actionPerformed(ActionEvent ae){
/*Finish this function*/
if(ae.getSource() == exit)
System.exit(0);
/*Creat a FileDialog for user to choose one file*/
if(ae.getSource() == file){
FileDialog fd = new FileDialog(this, "Open File", FileDialog.LOAD);
fd.setVisible(true);
if(fd.getFile() != null){
File f= new File(fd.getDirectory() + fd.getFile());
if(f.exists())
readFile(f.toString());
else
text.setText("The file does not exits");
}
fd.dispose();
}
/*Show some information about this function*/
}
/*Read file if you select one*/
public void readFile(String file){
text.setText("");
try{
BufferedReader read = new BufferedReader(new FileReader(file));
String line;
//line = read.readLine();
while((line = read.readLine()) != null)
text.append(line + "\n");
read.close();
} catch (IOException ioe) {
System.err.println(ioe);
}
}
public static void main(String args[]){
TextView t = new TextView();
}
}
这个程序运行正常,但把JTextArea换成TextArea就无法显示,另一个与此类此的程序中把Label换成JLabel也无法显示,求各位大虾解释一下 谢谢啊 展开
3个回答
展开全部
那是因为JTextArea的无参的构造函数会构造一个初始字符串为 null,行和列均为 0文本框,所以就显示不出来。你可以查看API
你可以指定初始的字符串或者指定大小,如下:
JTextArea text = new JTextArea(10, 20);
你可以指定初始的字符串或者指定大小,如下:
JTextArea text = new JTextArea(10, 20);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
把这两句:setVisible(true);
setSize(500, 500);
放在TextView()这个构造方法的最末尾那里。。
setSize(500, 500);
放在TextView()这个构造方法的最末尾那里。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询