Forum » Programiranje » [Java] kako spraviti program v pogon
[Java] kako spraviti program v pogon
pr2501 ::
Enaka verzija programa mi je delala (prikazala vse elemente), ko je bilo vse znotraj main bloka. Sedaj pa mi prikaze le glavni okvir in dva mala kvadratka noter (panel).
Program bi moral prikazati v glavnem okvirju dva panela: ploskev, ploskev1 z vsemi pripadajocimi elementi (gumbi, labelami,comboboxi).
Preikazemi pa le dva kvadratka, ki predvidevam da sta panela.
Mislim da nebom nic zgresil ce povem, da mi deluje le main del programa.
Kaj mi bi lahko gdo prosim razlozil te osnove ?
In takoj bom vprasal se eno stvar, ki mi bo dosti pomagala:
Kako se v ene projektu zdruzi vec razredov ?
V naprej hvala in zelim vam cimuspesnejse leto.
import java.awt.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.event.*;
public class sreda extends JPanel{
String izbira [] = {"10","20"};
JPanel ploskev, ploskev1;
// JLabel z_tem,h_tem ,c_tem ;
public sreda (){
try {
UIManager.setLookAndFeel (
UIManager.getCrossPlatformLookAndFeelClassName());
} catch (Exception e){}
//ploskev
JLabel z_tem = new JLabel("Zeljena tem");
JLabel h_tem = new JLabel("Dovoljeno odstopanje");
JLabel c_tem = new JLabel ("Kriticna tem");
JComboBox Tem_w = new JComboBox ();
//gumb.addActionListener(this);
for (int i=0; i< izbira.length; i++) {
Tem_w.addItem(izbira[i]);
// pogruntaj DODAJANJE SE ENEGA ELEMENTA
}
Tem_w.setEditable(true);
Tem_w.setSelectedIndex(1);
Tem_w.setMaximumRowCount(4);
ploskev.add(Tem_w);
JComboBox Tem_H = new JComboBox ();
for (int i=0; i< izbira.length; i++) {
Tem_H.addItem(izbira[i]);
}
Tem_H.setEditable(true);
Tem_H.setSelectedIndex(1);
Tem_H.setMaximumRowCount(4);
ploskev.add(Tem_H);
JComboBox Tem_C = new JComboBox ();
for (int i=0; i< izbira.length; i++) {
Tem_C.addItem(izbira[i]);
}
Tem_C.setEditable(true);
Tem_C.setSelectedIndex(1);
Tem_C.setMaximumRowCount(4);
ploskev.add(Tem_C);
ploskev.setLayout(new GridLayout(2,3,20,0));
ploskev.add(z_tem);
ploskev.add(h_tem);
ploskev.add(c_tem);
ploskev.add(Tem_w);
ploskev.add(Tem_H);
ploskev.add(Tem_C);
// ploskev1
JToggleButton gre = new JToggleButton ("GRELEC",false);
JToggleButton ven = new JToggleButton ("VENT", false);
JToggleButton STIKALO = new JToggleButton ("AVTOMATSKO", true);
ploskev1.add(STIKALO);
ploskev1.add(gre);
ploskev1.add(ven);
ploskev1.setLayout(new FlowLayout(FlowLayout.CENTER,19,10));
}
// frame
public static void main (String[] args){
JFrame okvir = new JFrame ("TIBET");
Container vsebina = okvir.getContentPane();
vsebina.setLayout(new FlowLayout(FlowLayout.CENTER,10,10));
JPanel ploskev1 = new JPanel ();
//ploskev1.setSize(50,50);
ploskev1.setBorder(new LineBorder (Color.gray));
JPanel ploskev = new JPanel ();
ploskev.setBorder(new LineBorder (Color.gray));
vsebina.add(ploskev1);
vsebina.add(ploskev);
okvir.setSize(600,300);
okvir.setVisible(true);
okvir.getContentPane().add(ploskev,BorderLayout.CENTER);
okvir.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
konec
Program bi moral prikazati v glavnem okvirju dva panela: ploskev, ploskev1 z vsemi pripadajocimi elementi (gumbi, labelami,comboboxi).
Preikazemi pa le dva kvadratka, ki predvidevam da sta panela.
Mislim da nebom nic zgresil ce povem, da mi deluje le main del programa.
Kaj mi bi lahko gdo prosim razlozil te osnove ?
In takoj bom vprasal se eno stvar, ki mi bo dosti pomagala:
Kako se v ene projektu zdruzi vec razredov ?
V naprej hvala in zelim vam cimuspesnejse leto.
import java.awt.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.event.*;
public class sreda extends JPanel{
String izbira [] = {"10","20"};
JPanel ploskev, ploskev1;
// JLabel z_tem,h_tem ,c_tem ;
public sreda (){
try {
UIManager.setLookAndFeel (
UIManager.getCrossPlatformLookAndFeelClassName());
} catch (Exception e){}
//ploskev
JLabel z_tem = new JLabel("Zeljena tem");
JLabel h_tem = new JLabel("Dovoljeno odstopanje");
JLabel c_tem = new JLabel ("Kriticna tem");
JComboBox Tem_w = new JComboBox ();
//gumb.addActionListener(this);
for (int i=0; i< izbira.length; i++) {
Tem_w.addItem(izbira[i]);
// pogruntaj DODAJANJE SE ENEGA ELEMENTA
}
Tem_w.setEditable(true);
Tem_w.setSelectedIndex(1);
Tem_w.setMaximumRowCount(4);
ploskev.add(Tem_w);
JComboBox Tem_H = new JComboBox ();
for (int i=0; i< izbira.length; i++) {
Tem_H.addItem(izbira[i]);
}
Tem_H.setEditable(true);
Tem_H.setSelectedIndex(1);
Tem_H.setMaximumRowCount(4);
ploskev.add(Tem_H);
JComboBox Tem_C = new JComboBox ();
for (int i=0; i< izbira.length; i++) {
Tem_C.addItem(izbira[i]);
}
Tem_C.setEditable(true);
Tem_C.setSelectedIndex(1);
Tem_C.setMaximumRowCount(4);
ploskev.add(Tem_C);
ploskev.setLayout(new GridLayout(2,3,20,0));
ploskev.add(z_tem);
ploskev.add(h_tem);
ploskev.add(c_tem);
ploskev.add(Tem_w);
ploskev.add(Tem_H);
ploskev.add(Tem_C);
// ploskev1
JToggleButton gre = new JToggleButton ("GRELEC",false);
JToggleButton ven = new JToggleButton ("VENT", false);
JToggleButton STIKALO = new JToggleButton ("AVTOMATSKO", true);
ploskev1.add(STIKALO);
ploskev1.add(gre);
ploskev1.add(ven);
ploskev1.setLayout(new FlowLayout(FlowLayout.CENTER,19,10));
}
// frame
public static void main (String[] args){
JFrame okvir = new JFrame ("TIBET");
Container vsebina = okvir.getContentPane();
vsebina.setLayout(new FlowLayout(FlowLayout.CENTER,10,10));
JPanel ploskev1 = new JPanel ();
//ploskev1.setSize(50,50);
ploskev1.setBorder(new LineBorder (Color.gray));
JPanel ploskev = new JPanel ();
ploskev.setBorder(new LineBorder (Color.gray));
vsebina.add(ploskev1);
vsebina.add(ploskev);
okvir.setSize(600,300);
okvir.setVisible(true);
okvir.getContentPane().add(ploskev,BorderLayout.CENTER);
okvir.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
konec
konec
JDK6 ::
Ena ideja,
package com.test;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
/**
* Demonstrates simple GUI program.
*
* @author JDK6
*
*/
public class FrameDemo extends JFrame {
public static void main(String[] args) {
FrameDemo demo = new FrameDemo();
demo.setVisible(true);
}
public FrameDemo() {
buttonPanel = new JPanel();
buttonGroup = new ButtonGroup();
addRadioButton("10 enot", 10);
addRadioButton("20 enot", 20);
add(buttonPanel);
setSize(PREFERED_WIDTH, PREFERED_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void addRadioButton(String string, final int i) {
JRadioButton button = new JRadioButton(string);
buttonGroup.add(button);
buttonPanel.add(button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Izbral si " + i);
selection = i;
}
});
selection = i;
button.setSelected(true);
}
private JPanel buttonPanel;
private ButtonGroup buttonGroup;
private int selection;
public int PREFERED_WIDTH = 200;
public int PREFERED_HEIGHT = 200;
}
Ce mas vrec razredov to ni problem ce so v istem direktoriju, preberi si v kaksni knjigi o paketih tam naj bi blo to razlozeno.
package com.test;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
/**
* Demonstrates simple GUI program.
*
* @author JDK6
*
*/
public class FrameDemo extends JFrame {
public static void main(String[] args) {
FrameDemo demo = new FrameDemo();
demo.setVisible(true);
}
public FrameDemo() {
buttonPanel = new JPanel();
buttonGroup = new ButtonGroup();
addRadioButton("10 enot", 10);
addRadioButton("20 enot", 20);
add(buttonPanel);
setSize(PREFERED_WIDTH, PREFERED_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void addRadioButton(String string, final int i) {
JRadioButton button = new JRadioButton(string);
buttonGroup.add(button);
buttonPanel.add(button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Izbral si " + i);
selection = i;
}
});
selection = i;
button.setSelected(true);
}
private JPanel buttonPanel;
private ButtonGroup buttonGroup;
private int selection;
public int PREFERED_WIDTH = 200;
public int PREFERED_HEIGHT = 200;
}
Ce mas vrec razredov to ni problem ce so v istem direktoriju, preberi si v kaksni knjigi o paketih tam naj bi blo to razlozeno.
Vredno ogleda ...
Tema | Ogledi | Zadnje sporočilo | |
---|---|---|---|
Tema | Ogledi | Zadnje sporočilo | |
» | [Java] Ne zažene appletaOddelek: Programiranje | 1205 (1138) | iggy |
» | [Java] Zagon appleta v HTMLOddelek: Programiranje | 1538 (1360) | Bela01 |
» | [JAVA] branje iz datotekeOddelek: Programiranje | 2404 (2039) | Bela01 |
» | [Java]VprašanjaOddelek: Programiranje | 2103 (1571) | Bela01 |
» | [Java] Deljenje in ostanekOddelek: Programiranje | 3079 (2663) | pr2501 |