我有 2 个类,一个扩展面板,另一个扩展框架。
第一类,面板(用于图纸):
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class GraficPne extends JPanel{
public int dim1 = 0, dim2 = 0;
public JLabel lblSem1GraficPne, lblSem2GraficPne;
public GraficPne(){
super();
this.setLayout(null);
this.setupComponents();
}
public void setupComponents(){
lblSem1GraficPne = new JLabel("Sem I");
lblSem2GraficPne = new JLabel("Sem II");
lblSem1GraficPne.setFont(new Font("Serif", Font.PLAIN, 12));
lblSem2GraficPne.setFont(new Font("Serif", Font.PLAIN, 12));
this.add(lblSem1GraficPne);
this.add(lblSem2GraficPne);
}
public void layComponents(int firstConv, int secondConv){
lblSem1GraficPne.setBounds(20, getHeight() - firstConv - 20, 100, 20);
lblSem2GraficPne.setBounds(100, getHeight() - secondConv - 20, 100, 20);
}
public void paint(Graphics g){
super.paint(g);
g.setColor(Color.BLACK);
g.drawRect(0, 0, getWidth()-1, getHeight()-1);
this.setBackground(Color.GREEN);
g.setColor(Color.RED);
g.fillRect(20, getHeight()-dim1, 40, dim1);
g.setColor(Color.BLUE);
g.fillRect(100, getHeight()-dim2, 40, dim2);
}
public void setData(int dim1, int dim2){
this.dim1 = dim1;
this.dim2 = dim2;
}
}
第二类,框架,用于在其上添加面板:
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class GraficFrm extends JFrame implements ActionListener{
private JButton btnApply;
private JLabel lblSem1, lblSem2;
private JTextField txtSem1, txtSem2;
private GraficPne ob; //the object from first class
private int firstConv = 0, secondConv = 0;
public GraficFrm() {
super("Grafic soft");
initSettings();
setupComponents();
}
private void initSettings() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(null);
this.setSize(400, 450);
this.setLocation(100, 100);
this.setResizable(false);
}
private void setupComponents() {
ob = new GraficPne();
lblSem1 = new JLabel("Sem I");
txtSem1 = new JTextField();
lblSem2 = new JLabel("Sem II");
txtSem2 = new JTextField();
btnApply = new JButton("Apply");
ob.setBounds(30, 50, 340, 200);
lblSem1.setBounds(30, 280, 100, 20);
txtSem1.setBounds(150, 280, 220, 20);
lblSem2.setBounds(30, 330, 100, 20);
txtSem2.setBounds(150, 330, 220, 20);
btnApply.setBounds(170, 380, 80, 30);
this.add(lblSem1);
this.add(ob);
this.add(txtSem1);
this.add(lblSem2);
this.add(txtSem2);
this.add(btnApply);
btnApply.addActionListener(this);
}
public void getConvertedValues(){
String txtField1 = txtSem1.getText();
firstConv = Integer.parseInt(txtField1);
String txtField2 = txtSem2.getText();
secondConv = Integer.parseInt(txtField2);
}
public void getHighValues(){
if((firstConv > 100) || (secondConv > 100)) {
JOptionPane.showMessageDialog(this, "Only values < 100 are allowed");
firstConv = 0;
secondConv = 0;
txtSem1.setText("");
txtSem2.setText("");
ob.removeAll();
ob.repaint();
}
}
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(btnApply)){
try{
new GraficFrm();
getConvertedValues();
getHighValues();
ob.setData(firstConv, secondConv);
ob.layComponents(firstConv,secondConv);
ob.repaint();
}catch(NumberFormatException ex1){
JOptionPane.showMessageDialog(this, "Only natural numbers are allowed");
}
}
}
public static void main(String[] args) {
GraficFrm frm = new GraficFrm();
frm.setVisible(true);
}
}
我要做的就是创建一个名为 getNegativeValues() 的新方法,该方法将通过按下 Apply 按钮调用,并且当 firstConv 或 secondConv 小于 0 时执行此方法。我的方法块 getNegativeValues( ),我想只保留面板的绿色背景,就像它在油漆(图形g)中一样,但要绘制一条中心水平线(g.drawLine(0,getHeight()/ 2,getWidth(),getHeight( )/2);)。然后,当我执行程序并输入时,假设第一个文本字段的值为 55,第二个文本字段的值为 -40,在我的中心水平线上绘制 55,在我的中心水平线上绘制 -40,0 点将在这条中心水平线上。
LE:@nIcE cOw 我将做一个简短的回顾:所以,首先,当我执行我的应用程序时,我只想显示我的绿色面板和那些有 2 个文本字段的 2 个标签,如我的示例所示(尝试按照我的示例,拜托)(一切都好,我已经这样做了,所以这部分完成了)。之后,当我完成这 2 个文本字段时,假设有 2 个正值,然后当我按下应用按钮时,就像在我的示例中一样(没有任何中心垂直线)(也已经这样做了)。一切都很好,但我的问题是:如果用负值完成这 2 个文本字段之一,而另一个用正值完成,并且在我按下应用按钮后,我希望我的应用程序能够识别一个输入的负值我的文本字段,因此,在我的绿色面板上绘制一条中心水平线(在我按下应用按钮后)并完全按照您在上传的第一张图片中所做的那样绘制这两个矩形。请帮助我,正如我所说,我会帮助你理解我想要做什么,而且我想说我真的很感谢你的努力,如果我(再次)感到困惑,我很抱歉(再次)。