gpt4 book ai didi

java - 我似乎无法弄清楚 GUI 的问题,X 和 Y 坐标有点偏离,我的高度和宽度输入也关闭了

转载 作者:行者123 更新时间:2023-12-01 17:09:52 25 4
gpt4 key购买 nike

这是我执行时的 GUI

This is my GUI when executed

这就是它应该的样子

This is what it should look like

当我在文本字段中输入值时,它似乎无法正确编译。要么是字段被切换,要么是我的 GUI 设置不正确。有人能帮我吗?我目前是编码的初学者。

项目主要代码


import java.awt.Color;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.lang.reflect.Field;
import javax.swing.JOptionPane;

public class Project1 extends javax.swing.JFrame {



public Project1() {
initComponents();
}



@SuppressWarnings("unchecked")

private void initComponents() {

jLayeredPane1 = new javax.swing.JLayeredPane();
jPanel1 = new javax.swing.JPanel();
jComboBox1 = new javax.swing.JComboBox<>();
jComboBox2 = new javax.swing.JComboBox<>();
jComboBox3 = new javax.swing.JComboBox<>();
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
jTextField3 = new javax.swing.JTextField();
jTextField4 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Shape Drawing"));

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 200, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 200, Short.MAX_VALUE)
);

jComboBox1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Rectangle", "oval" }));
jComboBox1.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
jComboBox1ActionPerformed(evt);
}

private void jComboBox1ActionPerformed(ActionEvent evt) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
});

jComboBox2.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Hollow", "Solid" }));
jComboBox2.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
jComboBox2ActionPerformed(evt);
}

private void jComboBox2ActionPerformed(ActionEvent evt) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
});

jComboBox3.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Black", "Red", "Orange", "Yellow", "Green", "Blue", "Magenta" }));

jTextField1.setText("100");
jTextField1.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}

private void jTextField1ActionPerformed(ActionEvent evt) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
});

jTextField2.setText("120");

jTextField3.setText("X");

jTextField4.setText("Y");

jButton1.setText("Draw");
jButton1.addActionListener((java.awt.event.ActionEvent evt) -> {
jButton1ActionPerformed(evt);
});

jLabel1.setFont(new java.awt.Font("Tahoma", 1, 10)); // NOI18N
jLabel1.setText("Shape Type");

jLabel2.setFont(new java.awt.Font("Tahoma", 1, 10)); // NOI18N
jLabel2.setText("Fill Type");

jLabel3.setFont(new java.awt.Font("Tahoma", 1, 10)); // NOI18N
jLabel3.setText("Color");

jLabel4.setFont(new java.awt.Font("Tahoma", 1, 10)); // NOI18N
jLabel4.setText("x coordinates");

jLabel5.setFont(new java.awt.Font("Tahoma", 1, 10)); // NOI18N
jLabel5.setText("y coordinates");

jLabel6.setFont(new java.awt.Font("Tahoma", 1, 10)); // NOI18N
jLabel6.setText("Width");

jLabel7.setFont(new java.awt.Font("Tahoma", 1, 10)); // NOI18N
jLabel7.setText("Height");

jLayeredPane1.setLayer(jPanel1, javax.swing.JLayeredPane.DEFAULT_LAYER);
jLayeredPane1.setLayer(jComboBox1, javax.swing.JLayeredPane.DEFAULT_LAYER);
jLayeredPane1.setLayer(jComboBox2, javax.swing.JLayeredPane.DEFAULT_LAYER);
jLayeredPane1.setLayer(jComboBox3, javax.swing.JLayeredPane.DEFAULT_LAYER);
jLayeredPane1.setLayer(jTextField1, javax.swing.JLayeredPane.DEFAULT_LAYER);
jLayeredPane1.setLayer(jTextField2, javax.swing.JLayeredPane.DEFAULT_LAYER);
jLayeredPane1.setLayer(jTextField3, javax.swing.JLayeredPane.DEFAULT_LAYER);
jLayeredPane1.setLayer(jTextField4, javax.swing.JLayeredPane.DEFAULT_LAYER);
jLayeredPane1.setLayer(jButton1, javax.swing.JLayeredPane.DEFAULT_LAYER);
jLayeredPane1.setLayer(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);
jLayeredPane1.setLayer(jLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);
jLayeredPane1.setLayer(jLabel3, javax.swing.JLayeredPane.DEFAULT_LAYER);
jLayeredPane1.setLayer(jLabel4, javax.swing.JLayeredPane.DEFAULT_LAYER);
jLayeredPane1.setLayer(jLabel5, javax.swing.JLayeredPane.DEFAULT_LAYER);
jLayeredPane1.setLayer(jLabel6, javax.swing.JLayeredPane.DEFAULT_LAYER);
jLayeredPane1.setLayer(jLabel7, javax.swing.JLayeredPane.DEFAULT_LAYER);

javax.swing.GroupLayout jLayeredPane1Layout = new javax.swing.GroupLayout(jLayeredPane1);
jLayeredPane1.setLayout(jLayeredPane1Layout);
jLayeredPane1Layout.setHorizontalGroup(
jLayeredPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jLayeredPane1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jLayeredPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jLayeredPane1Layout.createSequentialGroup()
.addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextField4))
.addGroup(jLayeredPane1Layout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jComboBox1, 0, 141, Short.MAX_VALUE))
.addGroup(jLayeredPane1Layout.createSequentialGroup()
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jComboBox2, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(jLayeredPane1Layout.createSequentialGroup()
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jComboBox3, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(jLayeredPane1Layout.createSequentialGroup()
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextField1))
.addGroup(jLayeredPane1Layout.createSequentialGroup()
.addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextField2))
.addGroup(jLayeredPane1Layout.createSequentialGroup()
.addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextField3)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(jLayeredPane1Layout.createSequentialGroup()
.addGap(224, 224, 224)
.addComponent(jButton1)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jLayeredPane1Layout.setVerticalGroup(
jLayeredPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jLayeredPane1Layout.createSequentialGroup()
.addGap(34, 34, 34)
.addGroup(jLayeredPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(jLayeredPane1Layout.createSequentialGroup()
.addGroup(jLayeredPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jLayeredPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(9, 9, 9)
.addGroup(jLayeredPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jLayeredPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel4))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jLayeredPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel5))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jLayeredPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel6))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jLayeredPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel7)))
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE)
.addComponent(jButton1))
);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLayeredPane1)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLayeredPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
);

pack();
}// </editor-fold>


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

int w = 0, h = 0, x = 0, y = 0;
try {
x = Integer.parseInt(jTextField1.getText());
y = Integer.parseInt(jTextField2.getText());
w = Integer.parseInt(jTextField3.getText());
h = Integer.parseInt(jTextField4.getText());
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Please enter numbers in the field", "Error", JOptionPane.WARNING_MESSAGE);
return;
}
Rectangle rec = new Rectangle(w, h, x, y);
String strco = jComboBox3.getSelectedItem().toString().toLowerCase();


Color color;
try {
Field field = Color.class.getField(strco);
color = (Color) field.get(null);
} catch (IllegalAccessException | IllegalArgumentException | NoSuchFieldException | SecurityException e) {
color = null;
}



boolean fill = true;


if (jComboBox2.getSelectedItem().toString().equals("Hollow")) {
fill = false;
}


if (jComboBox1.getSelectedItem().toString().equals("Rectangle")) {


Rectangular re = new Rectangular(rec, color, fill);
painting(re);


} else {


Oval ov = new Oval(rec, color, fill);
painting(ov);
}




}


public void painting(Shape shp) {


try {
Drawing d = new Drawing();
d.setSize(d.getPreferredSize());
d.drawShape(shp);
jPanel1.removeAll();
jPanel1.add(d);
jPanel1.repaint();
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "The shape it out of the panel", "Error", JOptionPane.WARNING_MESSAGE);
}


}

public static void main(String args[]) {

try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Project1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}

java.awt.EventQueue.invokeLater(() -> {
new Project1().setVisible(true);
});
}


// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JComboBox<String> jComboBox1;
private javax.swing.JComboBox<String> jComboBox2;
private javax.swing.JComboBox<String> jComboBox3;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLayeredPane jLayeredPane1;
private javax.swing.JPanel jPanel1;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
private javax.swing.JTextField jTextField4;
// End of variables declaration
}

绘图代码


import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel;


public class Drawing extends JPanel {


private Shape shape;


@Override
protected void paintComponent(Graphics grphcs) {
shape.Draw(grphcs);
grphcs.drawString("" + shape.getNoOfShapes(), 30, 30);
}


@Override
public Dimension getPreferredSize() {
Dimension dmnsn = new Dimension(200, 200);


return dmnsn;
//To change body of generated methods, choose Tools | Templates.
}


public void drawShape(Shape shape) throws Exception {


if (contains(shape.x, shape.y) && contains(shape.x, shape.y + shape.height)
&& contains(shape.x + shape.width, shape.y) && contains(shape.x + shape.width, shape.y + shape.height)) {
this.shape = shape;


repaint();
} else {
throw new OutsideBounds(111);
}
}

形状代码

package javafxapplication17;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;

public abstract class Shape extends Rectangle {

private final Color color;
private final boolean solid;
private static int count_shapes;

public Shape(Rectangle rctngl, Color color, boolean solid) {

this.color = color;
this.solid = solid;
this.setRect(rctngl.width, rctngl.height, rctngl.x, rctngl.y);
count_shapes++;
}

public void setColor(Graphics g) {
g.setColor(this.color);
}

public boolean getSolid() {
return this.solid;
}

public int getNoOfShapes() {
return count_shapes;
}

abstract void Draw(Graphics g);

}

椭圆形类


import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;

public class Oval extends Shape {

public Oval(Rectangle rctngl, Color color, boolean solid) {
super(rctngl, color, solid);
}


@Override
void Draw(Graphics g) {
setColor(g);
if (getSolid()) {
g.fillOval(this.width, this.height, this.x, this.y);
} else {
g.drawOval(this.width, this.height, this.x, this.y);
}

}

}

最佳答案

问题似乎出在这一行:

        Rectangle rec = new Rectangle(w, h, x, y);

documentation for the Rectangle constructor you are using表示参数的顺序为 xywidthheight,但您已交换了 width以及 x 和 y 的高度。

尝试将其替换为

        Rectangle rec = new Rectangle(x, y, w, h);

关于java - 我似乎无法弄清楚 GUI 的问题,X 和 Y 坐标有点偏离,我的高度和宽度输入也关闭了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61437163/

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