所以我只是在课堂上用Java启动GUI,我遇到了一个奇怪的问题。对于个人实践,我想制作一种简单的“电子表格”应用程序。现在我只是想让它显示列名。它就是这样,但就像我的JOptionPane.showInputDialog弹出框的图像一样。
以下是我的三个课程:
JPanel.java:
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Panel extends JPanel{
private Spreadsheet SprdSht;
public Panel(Spreadsheet sprdSht){
SprdSht = sprdSht;
}
public Panel(){
SprdSht = new Spreadsheet(0,0);
}
public void paintComponent(Graphics thing){
super.paintComponent(thing);
int width = getWidth();
int height = getHeight();
String temp[] = new String[100];
for(int x = 0; x<SprdSht.getCols(); x++){
temp[x] = JOptionPane.showInputDialog("Column " + (x+1) + " name?");
}
for(int x = 0; x<SprdSht.getCols(); x++){
thing.drawString(temp[x], x*50+5, 15);
}
}
}
TestSpreadsheet.java:
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class TestSpreadsheet {
public static void main(String args[]){
try{
String numColumnsStr = JOptionPane.showInputDialog("Number of columns?");
int numColumns = Integer.parseInt(numColumnsStr);
String numRowsStr = JOptionPane.showInputDialog("Number of rows?");
int numRows = Integer.parseInt(numRowsStr);
Spreadsheet sprdSht = new Spreadsheet(numColumns,numRows);
Panel tempGradePanel = new Panel(sprdSht);
JFrame tempFrame = new JFrame();
tempFrame.setDefaultCloseOperation(3);
tempFrame.add(tempGradePanel);
tempFrame.setSize(500, 500);
tempFrame.setVisible(true);
}
catch (Exception exc){
JOptionPane.showMessageDialog(null, "Bad response, closing program.");
}
}
}
Spreadsheet.java:
public class Spreadsheet {
private int Columns;
private int Rows;
public Spreadsheet(int cols, int rows){
Columns=cols;
Rows=rows;
}
public int getCols(){
return Columns;
}
public int getRows(){
return Rows;
}
}
我输入#columns后输入2,#rows输入1,第1列名称输入“column1”,第2列名称输入“column2”: http://i.stack.imgur.com/ykQrz.png