0

我有 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 个文本字段之一,而另一个用正值完成,并且在我按下应用按钮后,我希望我的应用程序能够识别一个输入的负值我的文本字段,因此,在我的绿色面板上绘制一条中心水平线(在我按下应用按钮后)并完全按照您在上传的第一张图片中所做的那样绘制这两个矩形。请帮助我,正如我所说,我会帮助你理解我想要做什么,而且我想说我真的很感谢你的努力,如果我(再次)感到困惑,我很抱歉(再次)。

4

1 回答 1

2

我想,您正在寻找的是一种如下所示的方法,它可以计算您尝试在JPanel. 这pos是正值和neg负值:

public void setValues(int above, int below)
{
    midPoint = getHeight() / 2;
    heightAbove = Math.abs(above);
    heightBelow = Math.abs(below);
    /*
     * If both are negative values, 
     * we need a Mid-Point in this case.
     */
    if (above < 0 && below < 0)
    {
        rectAbove = midPoint;
        diff = 120;
        rectBelow = midPoint;
        flag = true;
    }
    else if (above >= 0 && below >= 0)
    {
        rectAbove = getHeight() - heightAbove;
        diff = 120;
        rectBelow = getHeight() - heightBelow;
        flag = false;
    }
    else if (above < 0 && below >= 0)
    {
        rectAbove = midPoint;
        diff = 40;
        rectBelow = midPoint - heightBelow;
        flag = true;
    }
    else if (above >= 0 && below < 0)
    {
        rectAbove = midPoint - heightAbove;
        diff = 40;
        rectBelow = midPoint;
        flag = true;
    }       

    repaint();
}

这是代码:

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

public class PaintingRectangles
{
    private JTextField aboveField;
    private JTextField belowField;
    private JButton applyButton;
    private MyCanvas canvas;

    private void displayGUI()
    {
        JFrame frame = new JFrame("Painting Rectangles Example");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel contentPane = new JPanel();
        contentPane.setLayout(new BorderLayout(5, 5));

        JPanel rightPanel = new JPanel();
        rightPanel.setLayout(new GridLayout(3, 1, 5, 5));
        aboveField = new JTextField(10);
        belowField = new JTextField(10);
        applyButton = new JButton("Apply");
        applyButton.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent ae)
            {
                int above = 0;              
                int below = 0;
                try
                {
                    above = Integer.parseInt(aboveField.getText());
                    below = Integer.parseInt(belowField.getText());
                }
                catch(NumberFormatException nfe)
                {
                    aboveField.setText("0");
                    belowField.setText("0");
                    above = 0;
                    below = 0;
                    nfe.printStackTrace();
                }

                canvas.setValues(above, below);
            }
        });

        rightPanel.add(aboveField);
        rightPanel.add(belowField);
        rightPanel.add(applyButton);

        canvas = new MyCanvas();
        contentPane.add(canvas, BorderLayout.CENTER);
        contentPane.add(rightPanel, BorderLayout.LINE_END);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new PaintingRectangles().displayGUI();
            }
        });
    }
}

class MyCanvas extends JPanel
{   
    private int midPoint = getHeight() / 2;
    private int rectAbove = getHeight() / 2;
    private int rectBelow = getHeight() / 2;    
    private int width = 40;
    private int heightAbove = 0;
    private int heightBelow = 0;
    private int diff = 0;
    private boolean flag = false;

    public MyCanvas()
    {
        setBackground(Color.GREEN);
    }

    public void setValues(int above, int below)
    {
        midPoint = getHeight() / 2;
        heightAbove = Math.abs(above);
        heightBelow = Math.abs(below);
        /*
         * If both are negative values, 
         * we need a Mid-Point in this case.
         */
        if (above < 0 && below < 0)
        {
            rectAbove = midPoint;
            diff = 120;
            rectBelow = midPoint;
            flag = true;
        }
        else if (above >= 0 && below >= 0)
        {
            rectAbove = getHeight() - heightAbove;
            diff = 120;
            rectBelow = getHeight() - heightBelow;
            flag = false;
        }
        else if (above < 0 && below >= 0)
        {
            rectAbove = midPoint;
            diff = 40;
            rectBelow = midPoint - heightBelow;
            flag = true;
        }
        else if (above >= 0 && below < 0)
        {
            rectAbove = midPoint - heightAbove;
            diff = 40;
            rectBelow = midPoint;
            flag = true;
        }       

        repaint();
    }

    @Override
    public Dimension getPreferredSize()
    {
        return (new Dimension(400, 400));
    }

    @Override
    protected void paintComponent(Graphics g)
    {       
        super.paintComponent(g);
        g.setColor(Color.BLACK);
        g.drawRect(0, 0, getWidth(), getHeight());
        if (flag)
            g.drawLine(0, getHeight() / 2, getWidth(), getHeight() / 2);
        makeRectangles(g);
    }

    private void makeRectangles(Graphics g)
    {
        g.setColor(Color.RED);
        g.fillRect(40, rectAbove, width, heightAbove);

        g.setColor(Color.BLUE);
        g.fillRect(diff, rectBelow, width, heightBelow);

        g.dispose();
    }
}

这是输出:

矩形 矩形! 矩形2 矩形3

于 2013-05-02T15:41:10.997 回答