-1
任何人都可以請幫我解決這個問題。我有這個陣列的String
,我想在一個表中繪製不使用的JTable:在表中分配字符串
String inventory[][] = {{"Chairs","Tables"},{"Bed","Sofas"}}
我使用嵌套for
循環和drawLine
生成這個網格但我不能設法把String
在表內。誰能幫我?
它不是我的程序,但這是關閉的例子,我已經形成互聯網。我的程序有點類似這個。
import java.awt.* ;
import java.awt.event.* ;
import javax.swing.* ;
import javax.swing.border.TitledBorder;
import javax.swing.border.EtchedBorder;
import java.awt.*;
import java.awt.event.*;
public class testtable extends JFrame {
private static final int DRAWING_SIZE = 600;
private static final int SUBDIVISIONS = 2;
private static final int SUBDIVISION_SIZE = DRAWING_SIZE/SUBDIVISIONS;
public testtable() {
setSize(800, 800);
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.gridx = 0;
gbc.gridy = 0;
JLabel drawingBoard = new JLabel("Drawing Board");
gbc.anchor = GridBagConstraints.SOUTH;
drawingBoard.setFont(new Font("Serif", Font.BOLD, 28));
add(drawingBoard, gbc);
JPanel panel = new JPanel() {
@Override public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setPaint(Color.GRAY);
for (int i = 1; i < SUBDIVISIONS; i++) {
int x = i * SUBDIVISION_SIZE;
g2.drawLine(x, 0, x, getSize().height);
}
for (int i = 1; i < SUBDIVISIONS; i++) {
int y = i * SUBDIVISION_SIZE;
g2.drawLine(0, y, getSize().width, y);
}
}
};
panel.setPreferredSize(new Dimension(DRAWING_SIZE, DRAWING_SIZE));
panel.setBackground(Color.WHITE);
panel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
gbc.gridy++;
gbc.anchor = GridBagConstraints.CENTER;
add(panel, gbc);
JButton saveDrawing = new JButton("SAVE DRAWING");
gbc.gridy++;
gbc.anchor = GridBagConstraints.NORTH;
add(saveDrawing, gbc);
}
public static void main(String[] args) {
(new testtable()).setVisible(true);
}
}
你現在有什麼代碼?也許我們可以幫助你建立起它。 – 2012-02-06 22:37:39
對不起,我不能這麼做因爲我的論文的一部分,但這裏是我得到形式互聯網關閉的例子。它不完全一樣,但很接近。 – user1110191 2012-02-06 22:43:51
不過,你不提供樣品,所以我們能做的最好的事情就是猜測。所以我的猜測是你正在搞亂Graphics2D方法?那麼你可以簡單地使用drawString方法,不是嗎? – 2012-02-06 22:49:07