gpt4 book ai didi

java - 在绘画上分层按钮

转载 作者:行者123 更新时间:2023-12-01 08:14:11 24 4
gpt4 key购买 nike

有一个窗口,我想在其中放置一个按钮,然后绘制其下方的整个区域。换句话说,按钮应该覆盖一幅画。

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

class Window
{
private JFrame frame;
private JButton launchButton;
private JPanel pnllaunchButton;
private JPanel paintingPanel;
//width and height of client area
private Rectangle dim;

//w,h - width and height of the whole window
public Window(String title,int w,int h)
{
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(0,0);
frame.setMinimumSize( new Dimension(110,110));
frame.setSize(w, h);
frame.setVisible(true);
frame.setTitle(title);
frame.setResizable(false);

addLaunchButton();
}

private void addLaunchButton()
{
pnllaunchButton = new JPanel();
launchButton = new JButton("Plot!");
dim = new Rectangle();

frame.getContentPane().getBounds(dim);

pnllaunchButton.setBounds(dim.width-100,dim.height-25,100,25);

launchButton.setBounds(dim.width-100,dim.height-25,100,25);

pnllaunchButton.setLayout(null);

pnllaunchButton.add(launchButton);

frame.getContentPane().add(pnllaunchButton);
frame.getContentPane().setComponentZOrder(pnllaunchButton, new Integer(2));
}
public void drawCoordinateSystem()
{
paintingPanel = new JPanel();

paintingPanel.add(new CoordinateSystem());

frame.getContentPane().add(paintingPanel);
frame.getContentPane().setComponentZOrder(paintingPanel,new Integer(3));

}

}

class CoordinateSystem extends JPanel
{
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Dimension size = this.getSize();

g.setColor(Color.BLACK);
g.drawLine(0,size.height/2,size.width, size.height/2);

g.drawLine(size.width/2, 0, size.width/2, size.height);

}
}

public class GC {

public static void main(String[] args)
{
Window h = new Window("GC",800,600);
h.drawCoordinateSystem();
}
}

此代码不符合规范。程序创建一个空窗口并输出:

 run:
Exception in thread "main" java.lang.IllegalArgumentException: illegal component position
at java.awt.Container.checkAdding(Container.java:504)
at java.awt.Container.setComponentZOrder(Container.java:759)
at Window.addLaunchButton(Window.java:46)
at Window.<init>(Window.java:26)
at GC.main(GC.java:10)

你能指出我的错误吗? setComponentZOrder() javadoc中似乎没有精确描述方法。

最佳答案

  • 重命名您的类(class)。 Window 类已经是标准核心 Java 库的一部分,您的类名可能会导致当前或将来的问题。
  • 不要使用 null 布局和 setBounds(...)。这很糟糕,很糟糕,很糟糕,并且会使维护或升级您的应用程序变得非常困难。相反,了解并使用布局管理器。
  • 考虑将 JLabel 作为 contentPane,使其不透明,为其提供布局和 ImageIcon,然后向其中添加您的组件。
  • ImageIcon 可以保存带有网格的 BufferedImage。

例如:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import javax.swing.*;

public class MyWindow {
private static final int PREF_W = 800;
private static final int PREF_H = 600;
private static final Color COLOR0 = Color.red;
private static final Color COLOR1 = Color.blue;
private static final float COLOR_REPEAT_DIST = 30f;
private JLabel backGroundLabel = new JLabel();

public MyWindow() {
backGroundLabel.setOpaque(true);
backGroundLabel.setLayout(new BorderLayout());
int eb = 15;
BufferedImage bkgrndImg = createBkgrndImage();
ImageIcon icon = new ImageIcon(bkgrndImg);
backGroundLabel.setIcon(icon);

JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new FlowLayout(SwingConstants.RIGHT, eb, eb));
bottomPanel.setOpaque(false);
bottomPanel.add(new JButton("Plot"));
backGroundLabel.add(bottomPanel, BorderLayout.PAGE_END);
}

private BufferedImage createBkgrndImage() {
BufferedImage img = new BufferedImage(PREF_W, PREF_H, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = img.createGraphics();
g2.setPaint(new GradientPaint(0f, 0f, COLOR0, COLOR_REPEAT_DIST, COLOR_REPEAT_DIST, COLOR1, true));
g2.fillRect(0, 0, PREF_W, PREF_H);
g2.dispose();
return img;
}

public JComponent getMainPane() {
return backGroundLabel;
}

private static void createAndShowGui() {
MyWindow mainPanel = new MyWindow();

JFrame frame = new JFrame("MyWindow");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel.getMainPane());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}

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

看起来像这样:
enter image description here

关于java - 在绘画上分层按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14866389/

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