Java 2个JPanel,但不知道如何控制它们

Java 2个JPanel,但不知道如何控制它们,java,swing,applet,jpanel,layout-manager,Java,Swing,Applet,Jpanel,Layout Manager,嘿,我想做一个天气小程序,但我不明白每次j面板一个覆盖另一个这里是什么,我希望它看起来像在最后 在我得到这部分代码后,我将制作一个if-else语句,根据“季节”绘制图像 这在我的课堂上是一个很大的分数,请不要告诉我为什么老师不应该教CS或其他任何东西。这对我没有帮助。我在编程时使用Ready java,所以如果你能尝试保留相同的数据包,我非常缺乏经验,也很迷茫,但我需要在周一之前完成这项工作,并且已经为此工作了很长时间 好的,我正在尝试一个带有边框布局的新文件,我将创建下面的类,并尝试调用它

嘿,我想做一个天气小程序,但我不明白每次j面板一个覆盖另一个这里是什么,我希望它看起来像在最后

在我得到这部分代码后,我将制作一个if-else语句,根据“季节”绘制图像

这在我的课堂上是一个很大的分数,请不要告诉我为什么老师不应该教CS或其他任何东西。这对我没有帮助。我在编程时使用Ready java,所以如果你能尝试保留相同的数据包,我非常缺乏经验,也很迷茫,但我需要在周一之前完成这项工作,并且已经为此工作了很长时间

好的,我正在尝试一个带有边框布局的新文件,我将创建下面的类,并尝试调用它们,我猜

    public class Final
{

    JPanel jPanel1, jPanel2;
    JButton Enter, Exit;
    JTextField location;
    JLabel city;
    JRadioButton time;
    JComboBox Seasons;

    Random rand = new Random ();
    int P = rand.nextInt (100) + 1; //Random Precipitation
    int H = rand.nextInt (50) + 1; //Random Heat
    public void init ()
    {
        JPanel contentPane = getContentPane ();




        contentPane.setLayout (new BorderLayout ());
        contentPane.add (locationGui, BorderLayout.NORTH);
        contentPane.add (temp, BorderLayout.EAST);
        contentPane.add (Percip (g), BorderLayout.SOUTH);
        contentPane.add (image, BorderLayout.CENTER);
    }


public void drawPercip (Graphics g)
    {
        //Precipitation Bar
        g.setColor (Color.black);
        g.drawRect (40, 170, 100, 20); //outline of bar
        g.setColor (Color.blue);
        g.fillRect (40 + 1, 170 + 4, P, 14); //indicator bar (+4 puts space beetween outline bar)
    }
}
这是我现在使用的代码,我在其中获得了位置gui部件,但当我尝试使用tlabel=new DrawingPanel()时;它说这是不可能的,所以我怎么才能把那张图片带进来呢?我非常确定我需要使用actionlistener,所以我必须在稍后将其添加到实现中

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.util.*;

public class Final2 extends JFrame
{
    //Panels
    JPanel locationGui = new JPanel ();
    JPanel temp = new JPanel ();
    JPanel percip = new JPanel ();
    JPanel image = new JPanel ();
    //Location gui components
    JButton Enter, Exit;
    JTextField location;
    JLabel city;
    JRadioButton time;
    JComboBox Seasons;
    //bar # genertor
    Random rand = new Random ();
    int P = rand.nextInt (100) + 1; //Random Precipitation
    int H = rand.nextInt (50) + 1; //Random Heat
    public Final2 ()
    {
        init ();

    }


    public void init ()
    {
        Font font = new Font ("impact", Font.PLAIN, 20);
        //________________________________________________new panel____________________
        locationGui.setBackground (Color.RED);
        JLabel guiLabel = new JLabel ("");
        guiLabel.setFont (font);

        Enter = new JButton ("Enter");
        Exit = new JButton ("exit");



        city = new JLabel ("What city?");
        location = new JTextField (20); //location entry field


        Seasons = new JComboBox ();
        Seasons.addItem ("Summer");
        Seasons.addItem ("Fall");
        Seasons.addItem ("Winter");
        Seasons.addItem ("Spring");


        time = new JRadioButton ("check if night?");


        locationGui.add (city);
        locationGui.add (location);
        locationGui.add (Seasons);
        locationGui.add (time);



        locationGui.add (guiLabel);


        //________________________________________________new panel____________________
        temp.setBackground (Color.BLUE);
        temp.setLayout (new GridBagLayout ());
        JLabel tempLabel = new JLabel ("Temp");
        tempLabel.setFont (font);
        temp.add (tempLabel);

        //________________________________________________new panel____________________
        percip.setBackground (Color.GREEN);
        JLabel pLabel = new JLabel ("Percip");
        pLabel.setFont (font);
        percip.add (pLabel);

        //________________________________________________new panel____________________
        image.setBackground (Color.ORANGE);
        image.setLayout (new GridBagLayout ());
        JLabel imageLabel = new JLabel ("Image");
        imageLabel.setFont (font);
        image.add (imageLabel);

        Container contentPane = getContentPane ();

        contentPane.setLayout (new BorderLayout ());
        contentPane.add (locationGui, BorderLayout.NORTH);
        contentPane.add (temp, BorderLayout.EAST);
        contentPane.add (percip, BorderLayout.SOUTH);
        contentPane.add (image, BorderLayout.CENTER);

        setContentPane (contentPane);
        setDefaultCloseOperation (EXIT_ON_CLOSE);
        setSize (400, 400);
        setLocationRelativeTo (null);
        setVisible (true);
    }


    public static void main (String[] args)
    {
        SwingUtilities.invokeLater (new Runnable ()
        {

            public void run ()
            {
                new Final2 ();
            }
        }


        );
    }


    private class DrawingPanel extends javax.swing.JPanel
    {
        public void paint (Graphics g)
        {
            super.paint (g);

            Graphics2D g2d = (Graphics2D) g;

            drawPercip (g);
        }


    }


    public void drawPercip (Graphics g)
    {
        //Precipitation Bar
        g.setColor (Color.black);
        g.drawRect (40, 170, 100, 20); //outline of bar
        g.setColor (Color.blue);
        g.fillRect (40 + 1, 170 + 4, P, 14); //indicator bar (+4 puts space beetween outline bar)
    }


    public void drawTemp (Graphics g)
    {
        //Temparature Bar
        g.setColor (Color.red);
        g.fillRect (170 + 4, 50, 14, 100); //Covers in
        g.setColor (Color.black);
        g.drawRect (170, 50, 20, 100); //outline of bar
        g.setColor (Color.white);
        g.fillRect (170 + 4, 50 + 1, 16, 100 - H); //indicator bar (+4 puts space beetween outline bar)
    }




}

您发布的图像可以使用BorderLayout实现。建议阅读:


您发布的图像可以使用BorderLayout实现。建议阅读:

  • JApplet
    没有
    paintComponent
    方法,因此实际上没有重写任何内容。作为良好的实践,您应该使用
    @Override
    注释来确定您正在进行有效的覆盖<代码>JApplet确实有一个
    绘制
    方法
  • 不要在
    JPanel
    中覆盖
    paint
    。而是重写
    paintComponent
    并调用
    super.paintComponent
  • 不要使用空布局。Swing用于LayoutManager。看
  • 正如@KevinWorkman所建议的,您想要的布局是

  • 为您的新帖子更新

  • getContentPane()返回一个
    容器
    。您可以将其设置为=a
    JPanel
  • 您还没有声明任何我们可以使用的变量
  • 请参见下面的示例,了解
    BorderLayout
    如何适用于您的
    Final

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Font;
    import java.awt.GridBagLayout;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class Final extends JFrame{
    
        JPanel locationGui = new JPanel();
        JPanel temp = new JPanel();
        JPanel percip= new JPanel();
        JPanel image = new JPanel();
        public Final() {
            init();
    
        }
    
        public void init() {
            Font font = new Font("impact", Font.PLAIN, 20);
    
            locationGui.setBackground(Color.RED);
            JLabel guiLabel = new JLabel("Location Gui");
            guiLabel.setFont(font);
            locationGui.add(guiLabel);
    
            temp.setBackground(Color.BLUE);
            temp.setLayout(new GridBagLayout());
            JLabel tempLabel = new JLabel("Temp");
            tempLabel.setFont(font);
            temp.add(tempLabel);
    
            percip.setBackground(Color.GREEN);
            JLabel gLabel = new JLabel("Percip");
            gLabel.setFont(font);
            percip.add(gLabel);
    
            image.setBackground(Color.ORANGE);
            image.setLayout(new GridBagLayout());
            JLabel imageLabel = new JLabel("Image");
            imageLabel.setFont(font);
            image.add(imageLabel);
    
            Container contentPane = getContentPane();
    
            contentPane.setLayout(new BorderLayout());
            contentPane.add(locationGui, BorderLayout.NORTH);
            contentPane.add(temp, BorderLayout.EAST);
            contentPane.add(percip, BorderLayout.SOUTH);
            contentPane.add(image, BorderLayout.CENTER);
    
            setContentPane(contentPane);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setSize(400, 400);
            setLocationRelativeTo(null);
            setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable(){
                @Override
                public void run() {
                    new Final();
                }
            });
        }
    }
    


    更新到您的新编辑

    • 当您执行
      temp=new DrawingPanel()
      时,它可以工作,但存在两个问题
    • 面板没有预设尺寸,面板将仅遵循框架的
      边框布局
      中定义的尺寸。要解决此问题,请覆盖
      DrawingPanel的
      getPreferredSize
    • fillRect
      不显示,因为它与背景颜色相同
    以下是对代码的修复

    private class DrawingPanel extends javax.swing.JPanel {
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            drawPercip(g);
        }
        @Override 
        public Dimension getPreferredSize() {
            return new Dimension(200, 400);
        }
    }
    
    public void drawPercip(Graphics g) {
        g.setColor(Color.black);
        g.drawRect(40, 170, 100, 20);
        g.setColor(Color.blue);
        g.fillRect(40 + 1, 170 + 4, P, 14); 
    }
    
  • JApplet
    没有
    paintComponent
    方法,因此实际上没有重写任何内容。作为良好的实践,您应该使用
    @Override
    注释来确定您正在进行有效的覆盖<代码>JApplet
    确实有一个
    绘制
    方法
  • 不要在
    JPanel
    中覆盖
    paint
    。而是重写
    paintComponent
    并调用
    super.paintComponent
  • 不要使用空布局。Swing用于LayoutManager。看
  • 正如@KevinWorkman所建议的,您想要的布局是

  • 为您的新帖子更新

  • getContentPane()返回一个
    容器
    。您可以将其设置为=a
    JPanel
  • 您还没有声明任何我们可以使用的变量
  • 请参见下面的示例,了解
    BorderLayout
    如何适用于您的
    Final

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Font;
    import java.awt.GridBagLayout;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class Final extends JFrame{
    
        JPanel locationGui = new JPanel();
        JPanel temp = new JPanel();
        JPanel percip= new JPanel();
        JPanel image = new JPanel();
        public Final() {
            init();
    
        }
    
        public void init() {
            Font font = new Font("impact", Font.PLAIN, 20);
    
            locationGui.setBackground(Color.RED);
            JLabel guiLabel = new JLabel("Location Gui");
            guiLabel.setFont(font);
            locationGui.add(guiLabel);
    
            temp.setBackground(Color.BLUE);
            temp.setLayout(new GridBagLayout());
            JLabel tempLabel = new JLabel("Temp");
            tempLabel.setFont(font);
            temp.add(tempLabel);
    
            percip.setBackground(Color.GREEN);
            JLabel gLabel = new JLabel("Percip");
            gLabel.setFont(font);
            percip.add(gLabel);
    
            image.setBackground(Color.ORANGE);
            image.setLayout(new GridBagLayout());
            JLabel imageLabel = new JLabel("Image");
            imageLabel.setFont(font);
            image.add(imageLabel);
    
            Container contentPane = getContentPane();
    
            contentPane.setLayout(new BorderLayout());
            contentPane.add(locationGui, BorderLayout.NORTH);
            contentPane.add(temp, BorderLayout.EAST);
            contentPane.add(percip, BorderLayout.SOUTH);
            contentPane.add(image, BorderLayout.CENTER);
    
            setContentPane(contentPane);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setSize(400, 400);
            setLocationRelativeTo(null);
            setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable(){
                @Override
                public void run() {
                    new Final();
                }
            });
        }
    }
    


    更新到您的新编辑

    • 当您执行
      temp=new DrawingPanel()
      时,它可以工作,但存在两个问题
    • 面板没有预设尺寸,面板将仅遵循框架的
      边框布局
      中定义的尺寸。要解决此问题,请覆盖
      DrawingPanel的
      getPreferredSize
    • fillRect
      不显示,因为它与背景颜色相同
    以下是对代码的修复

    private class DrawingPanel extends javax.swing.JPanel {
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            drawPercip(g);
        }
        @Override 
        public Dimension getPreferredSize() {
            return new Dimension(200, 400);
        }
    }
    
    public void drawPercip(Graphics g) {
        g.setColor(Color.black);
        g.drawRect(40, 170, 100, 20);
        g.setColor(Color.blue);
        g.fillRect(40 + 1, 170 + 4, P, 14); 
    }
    

    首先调用setLayout(null),然后再调用setLayout(newflowlayout())。你想用哪一个?您几乎应该始终使用布局(而不是空布局),在这种情况下,在JPanel上调用setBounds()将不起作用。相反,请尝试调用setMinimumSize()和setPreferredSize()。推荐阅读:好吧,我希望按钮作为flow layout,我希望只保留另一个作为一个框架,我猜您首先调用setLayout(null),然后再调用setLayout(new FlowLayout())。你想用哪一个?您几乎应该始终使用布局(而不是空布局),在这种情况下,在JPanel上调用setBounds()将不起作用。相反,请尝试调用setMinimumSize()和setPreferredSize()。推荐阅读:好吧,我想要按钮作为流程布局,我希望只保留另一个作为一个框架,我猜好吧,这样我可以更容易地遵循一个新文件,并发布我在上面尝试过的内容,但我仍然是这样lost@CurtisSpence你有什么困惑吗?我强烈建议你从小处做起,一路往上爬。这是一个关于布局的教程,包括BorderLayout。我希望我能做到这一点,但这是一个最终的项目,我只是对我所学的东西画了一个空白。好吧,让我更容易遵循一个新的文件,并发布了我在上面尝试过的内容,但我仍然是这样lost@CurtisSpence你有什么困惑吗?我强烈建议你从小处做起,一路往上爬。是一个关于布局的教程,包括BorderLayout。我希望我可以这样做,但这是一个最终的项目,我只是对所学内容一窍不通。我不太了解你的观点,但我将尝试BorderLayout idk,我将如何添加其余的组件,尽管我想我会在底部创建g.draw方法并将它们调用到每个领域,但我也不明白我是怎么做到的=S@overide是