在 JPanel 中显示 Java 2D 图形内容



我正在尝试生成一个用户界面,虽然我的创作作为 JFrame 运行良好,但它没有在它的 JPanel 化身中显示我的基于线条的图形。如果有人能指出我在这里做错了什么,将不胜感激。

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.*;
public class Bowling_GUI_v9 extends JPanel {
public JLabel[] label1;
public JTextField[] text1;
public JComboBox[] combo1;

public JPanel displayPanel = new JPanel();
private int[] horz_coords = new int[] {8,60,580, 53,140,420,100,60,580,245,140,580,280,140,580,315,140,580,350,60,580,400,60,580};
private int[] vert_coords = new int[] {60,140,220,260,300,340,380,420,460,500,540,580};
public Bowling_GUI_v9() {
    displayPanel.setLayout(null);
    //displayPanel.setSize(800, 600);
    displayPanel.setSize(new Dimension(800,600));
    createUserInterface();

   }     
   public void drawLines(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    //super.paintComponent(g);
    int a = vert_coords.length;
    for(int i=0; i<a; i++){
         g2d.drawLine(1, vert_coords[i], 400, vert_coords[i]);
    }
    a = horz_coords.length;
    for(int i=0; i<a; i=i+3){
        g2d.drawLine(horz_coords[i],horz_coords[i+1],horz_coords[i],horz_coords[i+2]);
    }
}
   public void paint(Graphics g) {
    //super.paint(g);
    paint(g);
    drawLines(g);
   }
   private void createUserInterface(){
    //Container contentPane = getContentPane();
    //contentPane.setLayout(null);
    label1 = new JLabel[51];
    text1 = new JTextField[17];
    combo1 = new JComboBox[5];
    int text1counter = 0;
    int label1counter = 0;
    int combo1counter = 0;
    int popCounter = 0;
    int counter = 2;
    int h_pos =0;
    int v_pos =1;
    int flag = 0;
    int wide = 30;
    String[] populate = new String[] {"Team Name", "Ave", "HCP", "Name", "Game 1", "Game 2", "Game 3", "Total", "Won", "Scratch Total", "Lost", "Team HCP", "", "HCP Total Pin", "", "WINS"};
    int[] identifier = new int[] {1,2,2,1,1,1,1,1,1,1,2,2,4,3,3,3,2,8,1,2,1,2,2,2,2,9};
    label1[label1counter] = new JLabel();
    label1[label1counter].setText(populate[popCounter]);
    label1[label1counter].setBounds(8,60,80,10);
    displayPanel.add(label1[label1counter]);
    label1counter++;
    popCounter++;
    text1[text1counter] = new JTextField();
    text1[text1counter].setText("Enter Team Name Here");
    text1[text1counter].setBounds(101,60,200,20);
    displayPanel.add(text1[text1counter]);
    text1counter++;
    text1[text1counter] = new JTextField();
    text1[text1counter].setText("Lane");
    text1[text1counter].setBounds(351,60,40,20);
    displayPanel.add(text1[text1counter]);
    text1counter++;
    for(int a=0; a<10; a++){
        for(int b=0; b<7; b++){
            counter++;                
            switch(identifier[counter]){
                case 1:
                if (b == 2){ wide = 90; }
                else if (b == 6 && a == 0){ wide = 55; }
                else { wide = 30; }
                label1[label1counter] = new JLabel();
                label1[label1counter].setText(populate[popCounter]);
                label1[label1counter].setBounds(horz_coords[h_pos],vert_coords[v_pos]- 20,wide,20);
                displayPanel.add(label1[label1counter]);
                h_pos = h_pos + 3;
                label1counter++;
                popCounter++;
                break;
                case 2:
                label1[label1counter] = new JLabel();
                label1[label1counter].setText("0");
                label1[label1counter].setBounds(horz_coords[h_pos],vert_coords[v_pos] -20, 30, 20);
                displayPanel.add(label1[label1counter]);
                h_pos = h_pos + 3;
                label1counter++;
                break;
                case 3:
                text1[text1counter] = new JTextField();
                text1[text1counter].setText("0");
                text1[text1counter].setBounds(horz_coords[h_pos],vert_coords[v_pos] - 20,30,20);
                displayPanel.add(text1[text1counter]);
                text1counter++;
                h_pos = h_pos + 3;
                break;
                case 4:
                combo1[combo1counter] = new JComboBox();
                combo1[combo1counter].setBounds(horz_coords[h_pos],vert_coords[v_pos] - 20,30,20);
                displayPanel.add(combo1[combo1counter]);
                combo1counter++;
                h_pos = h_pos + 3;
                break;
                case 8:
                b--;
                flag++;
                counter = 9;
                break;
                case 9:
                b--;
                flag++;
                counter = 17;
                break;
            }                
        }
        v_pos++;
        h_pos=0;
        if(flag == 4){
            flag = 1;
            counter ++;
        }
    }
    label1[38].setText("");
    label1[45].setText("");  
}

}
  1. 不要覆盖 paint()。自定义绘制是通过覆盖paintComponent(...)来完成的,然后调用super.paintComponent(...)作为第一个语句。

  2. 您需要覆盖getPreferredSize()以返回面板的大小,以便布局管理员可以完成其工作。

  3. 不要使用 null 布局,也不要使用 setSize()。使用适当的布局管理器,布局管理器将确定您添加到面板的任何组件的大小/位置。

  4. 您不会显示实际将面板添加到框架的位置。

阅读有关自定义绘画的 Swing 教程中的部分,以获取更多信息和工作示例。换句话说,从一个小例子开始,并确保在进行复杂的绘画之前了解整个过程。

本教程还有一个关于您应该阅读Layout Managers的部分,您可以正确使用它们。

相关内容

  • 没有找到相关文章

最新更新