Hi,
I guess the last problem i sorted out..now i have another one....
I have written a code for drawing a graph in the main class which
includes code for textfield and a button named plot.
My problem is i am getting the graph and textfield/button one at a
time.Both the codes are in the same main class ,wondering why am i not
getting them in the same frame both at the same time??
My code is given below.what do i change in the code to get the
text/button and graph field in the same frame??Thnx for the help.
Rupesh
/*
* HydrophobicityViewer.java
*
* Created on December 16, 2006, 10:05 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package myapplics;
import javax.swing.*;
import biomolecule.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
*
* @author rupesh
*/
public class HydrophobicityViewer {
/** Creates a new instance of HydrophobicityViewer */
public HydrophobicityViewer() {
}
public static void main(String []args)
{
JFrame frame=new JFrame();
//The label and text fo entering the Sequence.
JLabel sLabel= new JLabel ("Sequence");
final int FIELD_WIDTH=40;
final JTextField sField=new JTextField(FIELD_WIDTH);
sField.setText("");
JButton button= new JButton("PLOT");
JPanel panel =new JPanel();
panel.add(sLabel);
panel.add(sField);
panel.add(button);
frame.add(panel);
final int FRAME_WIDTH =600;
final int FRAME_HEIGHT =700;
frame.setSize(FRAME_WIDTH,FRAME_HEIGHT);
RectangleComponent component = new RectangleComponent();
frame.add(component);
frame.setTitle("Hydrophobicity viewer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
/*
* RectangleComponent.java
*
* Created on December 16, 2006, 10:11 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package biomolecule;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.Rectangle;
import javax.swing.JPanel;
import javax.swing.JComponent;
/**
*
* @author rupesh
*/
public class RectangleComponent extends JComponent{
/** Creates a new instance of RectangleComponent */
public RectangleComponent() {
}
public void paintComponent(Graphics g)
{
Graphics2D g2=(Graphics2D)g;
Rectangle box=new Rectangle(100,300,400,35);
g2.draw(box);
g2.setColor(Color.BLACK);
g2.draw(box);
g2.drawString("4.0",100,300);
Rectangle box1=new Rectangle(100,335,400,35);
g2.draw(box1);
g2.setColor(Color.BLACK);
g2.draw(box1);
g2.drawString("3.0",100,335);
Rectangle box2=new Rectangle(100,370,400,35);
g2.draw(box2);
g2.setColor(Color.BLACK);
g2.draw(box2);
g2.drawString("2.0",100,370);
Rectangle box3=new Rectangle(100,405,400,35);
g2.draw(box3);
g2.setColor(Color.BLACK);
g2.draw(box3);
g2.drawString("1.0",100,405);
Rectangle box4=new Rectangle(100,440,400,35);
g2.draw(box4);
g2.setColor(Color.BLACK);
g2.draw(box4);
g2.drawString("0.0",100,440);
Rectangle box5=new Rectangle(100,475,400,35);
g2.draw(box5);
g2.setColor(Color.BLACK);
g2.draw(box5);
g2.drawString("-1.0",100,475);
Rectangle box6=new Rectangle(100,510,400,35);
g2.draw(box6);
g2.setColor(Color.BLACK);
g2.draw(box6);
g2.drawString("-2.0",100,510);
Rectangle box7=new Rectangle(100,545,400,35);
g2.draw(box7);
g2.setColor(Color.BLACK);
g2.draw(box7);
g2.drawString("-3.0",100,545);
g2.drawString("-4.0",100,580);
g2.drawString("50",150,580);
g2.drawString("100",200,580);
g2.drawString("150",250,580);
g2.drawString("200",300,580);
g2.drawString("250",350,580);
g2.drawString("300",400,580);
g2.drawString("350",450,580);
g2.drawString("400",500,580);
}
}
Andrew Thompson - 17 Dec 2006 08:49 GMT
...
> My problem is i am getting the graph and textfield/button one at a
> time.Both the codes are in the same main class ,wondering why am i not
> getting them in the same frame both at the same time??
Possibly because you are cobbling this together,
with little or no understanding of the underlying
layouts.
> My code is given below.what do i change in the code ...
Please study this document before posting more codes.
<http://www.physci.org/codes/sscce>
This problem could be cut down to less than 30 lines of
code, as opposed to the 180+ lines of code in two
public classes across two packages. You have little
chance that people are going to compile and run it,
and it is often too much code to 'scan by eye'..
>...to get the
> text/button and graph field in the same frame??Thnx for the help.
See below.
> /*
> * HydrophobicityViewer.java
[quoted text clipped - 37 lines]
> sField.setText("");
> JButton button= new JButton("PLOT");
// one way (of millions) to layout this GUI.
JPanel panel =new JPanel(new FlowLayout());
panel.add(sLabel);
panel.add(sField);
panel.add(button);
frame.add(panel, BorderLayout.NORTH);
final int FRAME_WIDTH =600;
final int FRAME_HEIGHT =700;
frame.setSize(FRAME_WIDTH,FRAME_HEIGHT);
RectangleComponent component = new RectangleComponent();
frame.add(component, BorderLayout.CENTER);
frame.setTitle("Hydrophobicity viewer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
Andrew T.