繁体   English   中英

我无法在我的 JFrame 中显示图形

[英]Im having trouble trying to display graphics in my JFrame

我正在尝试创建具有与 Wordle 类似的布局的游戏,其中显示了一个 5 个字母的加扰词,在其下方有 5 个单独的文本字段供用户输入加扰词的字母将进入的顺序。基本上他们有解读这个词。 抱歉解释不好。

我试图在 JLabels lb1、lb2、lb3、lb4、lb5 的每个字母周围以及每个 JTextField tf1、tf2、tf3、tf4、tf5 周围制作矩形/正方形。 我根本不知道如何显示这些矩形,也不知道它们将如何显示在已经存在的 JPanel 之上。

这里也只是一个奖励,是否有组织这些标签和文本字段以便我可以在那里设置确切的位置? 我想在 5 个文本字段的顶部有 5 个标签。

import java.awt.*;
import javax.swing.*;



public class scramble extends JFrame {

    Font mainFont = new Font("Impact", Font.BOLD, 60);
    Font smallFont = new Font("Impact", Font.BOLD, 30);

    JLabel lbWelcome;
    JLabel lb1, lb2, lb3, lb4, lb5;
    JTextField tf1, tf2, tf3, tf4, tf5;
    JFrame frame;
    GridBagConstraints gbc = new GridBagConstraints();
    


    scramble() {

      gbc.gridx = 0;
      gbc.gridy = 0;
      gbc.anchor = GridBagConstraints.PAGE_START;
      gbc.weightx = 1.0;
      gbc.weighty = 1.0;

         //mainPanel
         JPanel mainPanel = new JPanel();
         mainPanel.setLayout(new GridBagLayout());
         mainPanel.setBackground(new Color(12, 177, 237));
         mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 40, 30, 40));
         
         

         //header/title
         lbWelcome = new JLabel("Scramble", SwingConstants.CENTER);
         lbWelcome.setFont(mainFont);
         lbWelcome.setToolTipText("Scramble is a game created by Glen Filson");
         gbc.gridx = 0;
         gbc.gridy = 0;
         mainPanel.add(lbWelcome, gbc);

          //input panel
          JPanel inputs = new JPanel();
          inputs.setSize(new Dimension(300,300));
          inputs.setBackground(new Color(12, 177, 200));
          inputs.setLayout(new GridBagLayout());
          gbc.gridx = 0;
          gbc.gridy = 3;
          mainPanel.add(inputs, gbc);

          gbc.insets = new Insets(30, 10,30,10);
        
          
         //JLabels main
         lb1  = new JLabel("F", SwingConstants.CENTER);
         lb1.setFont(smallFont);
         gbc.gridx = 0;
         gbc.gridy = 0;
         inputs.add(lb1, gbc);
      

         lb2  = new JLabel("I", SwingConstants.CENTER);
         lb2.setFont(smallFont);
         gbc.gridx = 1;
         gbc.gridy = 0;
         inputs.add(lb2, gbc);
        

         lb3  = new JLabel("G", SwingConstants.CENTER);
         lb3.setFont(smallFont);
         gbc.gridx = 2;
         gbc.gridy = 0;
         inputs.add(lb3, gbc);
         

         lb4  = new JLabel("H", SwingConstants.CENTER);
         lb4.setFont(smallFont);
         gbc.gridx = 3;
         gbc.gridy = 0;
         inputs.add(lb4, gbc);
        

         lb5  = new JLabel("T", SwingConstants.CENTER);
         lb5.setFont(smallFont);
         gbc.gridx = 4;
         gbc.gridy = 0;
         inputs.add(lb5, gbc);


         
         

         //JTextField main
         tf1  = new JTextField("s");
         tf1.setFont(smallFont);
         tf1.setHorizontalAlignment(JTextField.CENTER);
         gbc.gridx = 0;
         gbc.gridy = 1;
         inputs.add(tf1, gbc);
         


         tf2  = new JTextField("u");
         tf2.setFont(smallFont);
         tf2.setHorizontalAlignment(JTextField.CENTER);
         gbc.gridx = 1;
         gbc.gridy = 1;
         inputs.add(tf2, gbc);
       
         

         tf3  = new JTextField("s");
         tf3.setFont(smallFont);
         tf3.setHorizontalAlignment(JTextField.CENTER);
         gbc.gridx = 2;
         gbc.gridy = 1;
         inputs.add(tf3, gbc);
        

         tf4  = new JTextField("s");
         tf4.setFont(smallFont);
         tf4.setHorizontalAlignment(JTextField.CENTER);
         gbc.gridx = 3;
         gbc.gridy = 1;
         inputs.add(tf4, gbc);
         
         
         tf5  = new JTextField("y");
         tf5.setFont(smallFont);
         tf5.setHorizontalAlignment(JTextField.CENTER);
         gbc.gridx = 4;
         gbc.gridy = 1;
         inputs.add(tf5, gbc);

     
        
         

          
        



           
           
            //JFrame
            JFrame frame = new JFrame();      
            frame.setTitle("Scramble");
            frame.setSize(500, 650);
            
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            frame.getContentPane().setBackground(new Color(100, 60, 100));
            frame.setVisible(true);
            frame.add(mainPanel);
           
    }

    public static void main(String[] args){
       scramble myFrame = new scramble();
      
     

}
public class graphicsPanel extends JPanel {
   public void paint(Graphics g ){
      
      Graphics g2d = (Graphics2D) g;

      g2d.drawRect(250,325,300,300);

   }



}
}

您提出的所有问题都有解决方案。 所以让我们看看从哪里拿它。

  1. 您可以为任何 JComponent 及其子类添加边框。 只需使用setBorder()
  2. 要在每个字母周围有一个带有矩形的 JLabel,最好不要使用 JLabel。 子类 JComponent,添加一个 String 属性,以便您知道要呈现哪些文本,然后覆盖paintComponent()方法。 您只需要遍历字符串的字符,通过 GraphicsContext 和 Font 计算字符大小,为您的框添加空间。 然后使用g.drawRect() 绘制框,最后使用 g.drawString() 绘制
  3. 要组织位置,请使用 LayoutMangers。 GridBagLayout是最强大的,它会做你需要的任何事情。 查看不错的教程

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM