Speed
Posts: 2
Nickname: jparis
Registered: Dec, 2002
|
|
please help me !
|
Posted: Dec 8, 2002 6:54 AM
|
|
Advertisement
|
Please I will like to fill the second JPanel of my Frame that containts a JEditorPane with points so that it should look like.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . etc..
Can you tell what is wrong in my paint() method ? The important path is in the Commentary. Thanks in avance.
This is my Code.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyFrame extends JFrame {
private BorderLayout layout = new BorderLayout();
private JPanel jPanel1 = new JPanel();
private JPanel jPanel2 = new JPanel();
private JPanel jPanel3 = new JPanel();
private JPanel jPanel4 = new JPanel();
private JPanel jPanel5 = new JPanel();
private JEditorPane jEditorPane1 = new JEditorPane();
private JButton jButton1 = new JButton();
private JButton jButton2 = new JButton();
private JButton jButton3 = new JButton();
private JButton jButton4 = new JButton();
private JButton jButton5 = new JButton();
private JButton jButton6 = new JButton();
private JButton jButton7 = new JButton();
private JLabel jLabel1 = new JLabel("RennFahrer");
private JLabel jLabel2 = new JLabel("Virtueller Fahrer");
private JTextField jTextField1 = new JTextField();
private JTextField jTextField2 = new JTextField();
private JLabel statusBar = new JLabel("Ready");
private JFileChooser fc = new JFileChooser();
public MyFrame() {
initGUI();
pack();
}
/** This method is called from within the constructor to initialize the form. */
private void initGUI() {
jEditorPane1.setEditable(false);
jEditorPane1.setPreferredSize(new java.awt.Dimension(520, 620));
jEditorPane1.setMinimumSize(new java.awt.Dimension(520, 620));
jEditorPane1.setSize(new java.awt.Dimension(520, 620));
getContentPane().setLayout(null);
getContentPane().add(jPanel1);
getContentPane().add(jPanel2);
getContentPane().add(jPanel3);
// set title
setTitle("FormularOneGUI");
getContentPane().add(statusBar, BorderLayout.SOUTH);
// add menu bar
JMenuBar menuBar = new JMenuBar();
JMenu menuFile = new JMenu("Menu");
menuFile.setMnemonic('F');
// create Exit menu item
JMenuItem fileExit = new JMenuItem("Exit");
fileExit.setMnemonic('E');
fileExit.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
// create About menu item
JMenu menuHelp = new JMenu("Help");
menuHelp.setMnemonic('H');
JMenuItem helpAbout = new JMenuItem("About");
helpAbout.setMnemonic('A');
helpAbout.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
AboutDialog aboutDialog = new AboutDialog(MyFrame.this, true);
Dimension frameSize = getSize();
Dimension aboutSize = aboutDialog.getPreferredSize();
int x = getLocation().x + (frameSize.width - aboutSize.width) / 2;
int y = getLocation().y + (frameSize.height - aboutSize.height) / 2;
if (x < 0) x = 0;
if (y < 0) y = 0;
aboutDialog.setLocation(x, y);
aboutDialog.setVisible(true);
}
});
menuHelp.add(helpAbout);
// create Save menu item
JMenuItem saveFile = new JMenuItem("Save");
saveFile.setMnemonic('S');
saveFile.addActionListener(
new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
int returnVal = fc.showSaveDialog(MyFrame.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
java.io.File file = fc.getSelectedFile();
// Write your code here what to do with selected file
} else {
// Write your code here what to do if user has canceled Save dialog
}
}
});
menuFile.add(saveFile);
// create Print menu item
JMenuItem print = new JMenuItem("Print");
print.setMnemonic('P');
print.addActionListener(
new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
PrintDialog.print();
}
});
menuFile.add(print);
menuFile.add(fileExit);
menuBar.add(menuFile);
menuBar.add(menuHelp);
// sets menu bar
setJMenuBar(menuBar);
setBounds(new java.awt.Rectangle(0, 0, 579, 604));
addWindowListener(
new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
jPanel1.setBounds(new java.awt.Rectangle(24, -1, 524, 33));
jPanel1.add(jButton1);
jPanel1.add(jButton2);
jPanel1.add(jButton3);
jPanel1.add(jButton4);
jPanel1.add(jButton5);
jPanel1.add(jButton6);
jPanel2.setBounds(new java.awt.Rectangle(2, 38, 568, 473));
jPanel2.setMinimumSize(new java.awt.Dimension(510,600));
jPanel2.setPreferredSize(new java.awt.Dimension(510,600));
jPanel2.add(jEditorPane1);
jPanel3.setBounds(new java.awt.Rectangle(2, 514, 604, 42));
jPanel4.add(jLabel1);
jPanel4.add(jTextField1);
jPanel3.add(jPanel4);
jPanel5.add(jLabel2);
jPanel5.add(jTextField2);
jPanel5.add(jButton7);
jPanel3.add(jPanel5);
jButton1.setText("RennStrecke");
jButton2.setText("StartLinie");
jButton3.setText("Spielregeln");
jButton4.setText("Spiel Starten");
jButton5.setText("Info");
jButton6.setText("Help");
jButton7.setText("Fahrer Auswahl");
jButton5.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
jButton5.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
jTextField2.setMinimumSize(new java.awt.Dimension(4, 20));
jTextField2.setPreferredSize(new java.awt.Dimension(40, 20));
jTextField2.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
jTextField1.setPreferredSize(new java.awt.Dimension(40,20));
jTextField1.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
jLabel1.setFont(new java.awt.Font("SansSerif", java.awt.Font.BOLD, 12));
jLabel1.setVerticalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
jLabel2.setFont(new java.awt.Font("SansSerif", java.awt.Font.BOLD, 12));
jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
}
/* This a what i did but with no success
-----------------------------------------
public void paint(Graphics g){
g.setColor(Color.blue);
int width = jEditorPane1.getWidth();
int height = jEditorPane1.getHeight();
for(int i=0;i<width;i=i+5){
for(int j=0;j<height;j=j+5){
g.drawLine(getX()+5,getY()+5,getX()+5,getY()+5);
}
g.drawLine(getX()+5,getY()+5,getX()+5,getY()+5);
}
}
*/
}
And here is the Test Application
---------------------------------
import javax.swing.UIManager;
import java.awt.*;
public class MyGUI {
public MyGUI() {
MyFrame frame = new MyFrame();
// FahrerAuswahl auswahl = new FahrerAuswahl();
// auswahl.resize(500,400);
frame.resize(580,600);
//Center the frame on screen
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
frameSize.height = ((frameSize.height > screenSize.height) ? screenSize.height : frameSize.height);
frameSize.width = ((frameSize.width > screenSize.width) ? screenSize.width : frameSize.width);
frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
}
public static void main(String[] argv) {
// set up system Look&Feel
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) {
e.printStackTrace();
}
new MyGUI();
}
}
private void exitForm(WindowEvent evt) {
System.exit(0);
}
}
|
|