gpt4 book ai didi

java - 2个jpanel但不知道如何控制它们

转载 作者:行者123 更新时间:2023-12-01 18:37:27 25 4
gpt4 key购买 nike

嘿,我正在尝试制作一个天气小程序,但我不明白 j 面板每次覆盖另一个面板时我希望它最终看起来像这样

enter image description here

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

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.util.*;
public class Test21 extends JApplet
{
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

/** Initializes the applet Test02 */
public void init ()
{
jPanel2 = new DrawingPanel ();

getContentPane ().setLayout (null);


//);

getContentPane ().add (jPanel2);
jPanel2.setBounds (20, 20, 320, 240);




jPanel1 = new DrawingPanel();

getContentPane ().setLayout (null);

jPanel1.setBackground (new java.awt.Color (255, 255, 255));
//);

getContentPane ().add (jPanel1);
jPanel1.setBounds (20, 20, 320, 240);
getContentPane() .setLayout (new FlowLayout ());

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?");


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


}

public void paintComponent (Graphics g)
{
super.paint (g);

Graphics2D g2d = (Graphics2D) g;

g2d.drawLine (10, 70, 70, 10);

}

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

Graphics2D g2d = (Graphics2D) g;

drawBars (g);
}
}
public void drawBars (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)

//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)
}



}

这是我类的一个大标志,请不要告诉我为什么老师不应该教 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(); 时它说无法完成,那么我该如何将该图形引入?我很确定我需要使用 Action 监听器,所以我必须稍后将其添加到实现中

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)
}




}

最佳答案

  1. JApplet 没有没有 paintComponent 方法,因此您实际上并没有覆盖任何内容。作为良好实践,您应该使用 @Override 注释来确保您正在进行有效的覆盖。 JApplet 确实有一个paint方法。
  2. 不要覆盖 JPanel 中的paint。而是重写 paintComponent 并调用 super.paintComponent
  3. 不要使用空布局。 Swing 是为了与 LayoutManager 一起使用而设计的。请参阅Laying out components in a Container
  4. 正如 @KevinWorkman 所建议的,您想要的布局是 BorderLayout
<小时/>

更新您的新帖子

  1. getContentPane() 返回一个Container。你可以让它=一个JPanel
  2. 您尚未声明任何我们可以使用的变量

请参阅下面的示例,了解 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();
}
});
}
}

enter image description here

<小时/>

更新

到您的新编辑

  • 当您执行 temp = new DrawingPanel() 时,它可以工作,但有两个问题
    1. 面板没有首选尺寸,面板将仅遵循框架的 BorderLayout 中定义的尺寸。要解决此问题,请覆盖 DrawingPanelgetPreferredSize
    2. 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);
}

关于java - 2个jpanel但不知道如何控制它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21311954/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com