为什么我要获得两个标签和两个文本框 [英] why iam getting two labels and two textboxes

查看:47
本文介绍了为什么我要获得两个标签和两个文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我编写的代码

here is the code that i have written

import javax.swing.*;
import java.awt.*;
public class resistancecalc extends JPanel 
{
	 public void paintComponent(Graphics g)
    {    setLayout(new FlowLayout());
    
    	super.paintComponents(g);
    	
    	g.setColor(Color.PINK);
    	g.setFont(new Font("Monospaced",Font.BOLD+Font.ITALIC,24));
    	g.drawString("RESISTANCE CALACULATOR USING COLOR CODE", 180, 70);
    	g.setColor(Color.BLACK);
    	g.setFont(new Font("Serif",Font.BOLD+Font.ITALIC,18));
    	g.drawString("Description:",5,95);
    	g.setFont(new Font("Serif",Font.ITALIC,18));
    	g.drawString("This application calculates the resistance of the resistor using color code.Enter the color code and get the resistance",100,95); 
    	g.drawString("Its a very handy tool for electrical,electronics and other engineering students and technicians ",5,110);
        g.drawLine(450, 200, 300, 200);//first horizontal line
        g.setColor(Color.white);
        g.fillRect(405, 160,55 ,90);//first rectangle
        g.setColor(Color.BLACK);
        g.fillRect(461, 158,5, 95);//first border
        g.setColor(Color.WHITE);
        g.fillRect(468,160,55,90);//2nd rectangle
        g.setColor(Color.BLACK);
        g.fillRect(520,160,5,90);//2nd border
        g.setColor(Color.WHITE);
        g.fillRect(525, 160, 55, 90);//3rd rectangle
        g.setColor(Color.BLACK);
        g.fillRect(580, 160, 5, 90);//3rd border
        g.setColor(Color.WHITE);
        g.fillRect(585,160,55,90);//4th rectangle
        g.setColor(Color.black);
        g.drawLine(740, 195, 640, 195);//last horizontal line
        
        g.setColor(Color.yellow);
        g.drawLine(730, 72, 158, 72);//first underline
        g.drawLine(730, 75, 158, 75);//2nd underline
       
        JLabel lb1R= new JLabel("Enter the code in 4 digits");
        lb1R.setBounds(80, 300, 400, 18);
     
        lb1R.setToolTipText("This is label 1");
        
        add(lb1R);
        JTextField txt1R=new JTextField();
        txt1R.setToolTipText("This is textbox1");
        txt1R.setBounds(225, 300, 300, 25);
        
        
        add(txt1R);
        
        
        
        
       
         
        
        
     	
	    }
}


public static void main(String[] args) 
	{
	    Container c=new Container();
	    c.setLayout(new FlowLayout());
	    
	    
	    resistancecalc objR1=new resistancecalc();
	    JFrame frameR=new JFrame("Resistance Calculator Using Color Code");
	    frameR.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    frameR.add(objR1);
	    frameR.setSize(900,500);
	    frameR.setVisible(true);


这段代码带来的问题是,我得到了两个标签和两个文本框
我在框架的顶部,另一个在我指定的位置.我该怎么办?


the problem m getting with this code is that m getting two labels and two textboxes
i at the top of the frame and the other at the position i have specified. what should i do?

推荐答案

您已经在paintComponent()方法中添加了所有代码,因此每次绘制窗口时,它都会重建整个窗口,并再次添加组件.您需要分离出与创建对象有关的代码部分,并仅在该方法中使用绘图代码.尝试在首次显示窗口时调整其大小,以获取如何使屏幕完全混乱的示例.
You have added all your code in the paintComponent() method so every time the window is painted it rebuilds the entire window, adding components again. You need to separate out the parts of the code concerned with creating objects and use only drawing code in that method. Try resizing the window when it first displays for an example of how it can make a total mess of the screen.


这篇关于为什么我要获得两个标签和两个文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆