gpt4 book ai didi

Java Netbeans - 如何在 Jpanel 内的 Jpanel 上绘图? (面板构造函数)

转载 作者:行者123 更新时间:2023-12-01 09:35:29 24 4
gpt4 key购买 nike

我正在尝试学习 Java,现在我遇到了 Jpanels,而且由于我使用 Netbeans,我可以使用它们的“构造函数”,在其中我可以在框架内设计我想要的面板。

使用 netbeans,我在面板内创建了一个面板,现在我想知道:

如何在面板内的面板上绘图?

(我想在其上绘图的面板是EvoPanel)

This is my current design

深灰色框是面板内的第二个面板,如果我尝试从左上角到右下角画一条线,我最终会得到 this

我的代码:

Vindue.java

package vindue;
import javax.swing.*;

/**
*
* @author BE56df
*/
public class Vindue {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here

paneler panel = new paneler();

JFrame vindue = new JFrame("NEZ - Evolution Simulator v0.1");
vindue.add(panel);

vindue.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

vindue.setResizable(false);
vindue.pack();
vindue.setVisible(true);

}

}

Paneler.java

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

/**
*
* @author BE56df
*/
public class paneler extends javax.swing.JPanel {

/**
* Creates new form paneler
*/
public paneler() {
initComponents();
}

public void paintComponent(Graphics g){

super.paintComponent(g);

g.drawLine(0, 0, 750, 550);

}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

EvoPanel = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jSeparator1 = new javax.swing.JSeparator();
jToggleButton1 = new javax.swing.JToggleButton();

setMaximumSize(new java.awt.Dimension(750, 550));
setMinimumSize(new java.awt.Dimension(750, 550));
setPreferredSize(new java.awt.Dimension(750, 550));

EvoPanel.setBackground(new java.awt.Color(200, 200, 200));
EvoPanel.setMaximumSize(new java.awt.Dimension(550, 550));
EvoPanel.setMinimumSize(new java.awt.Dimension(550, 550));

javax.swing.GroupLayout EvoPanelLayout = new javax.swing.GroupLayout(EvoPanel);
EvoPanel.setLayout(EvoPanelLayout);
EvoPanelLayout.setHorizontalGroup(
EvoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 550, Short.MAX_VALUE)
);
EvoPanelLayout.setVerticalGroup(
EvoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 550, Short.MAX_VALUE)
);

jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
jLabel1.setText("Control panel");
jLabel1.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
jLabel1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

jToggleButton1.setText("Auto play");

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(EvoPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGap(63, 63, 63)
.addComponent(jLabel1)
.addGap(0, 58, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 190, Short.MAX_VALUE)
.addComponent(jToggleButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(EvoPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jToggleButton1))
);
}// </editor-fold>


// Variables declaration - do not modify
private javax.swing.JPanel EvoPanel;
private javax.swing.JLabel jLabel1;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JToggleButton jToggleButton1;
// End of variables declaration
}

最佳答案

显然,当您确实想在 EvoPanel 上绘制时,您可以在主 JFrame(面板面板)上绘制线条。您应该创建一个 EvoPanel 类(带有 paintComponent 方法)并在 paneler#initComponents 之后添加。

public paneler() {
initComponents();
EvoPanel evoPanel = new EvoPanel();
add(evoPanel) // properly in a layout
}

建议:对于简单的 GUI,不要使用 Mantisse(Netbeans GUI 构建工具),它不会给您带来任何好处。

关于Java Netbeans - 如何在 Jpanel 内的 Jpanel 上绘图? (面板构造函数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38990036/

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