- Menu nie chcę się wyświetlić tzn . MenuBar
Z góry chciałem zaznaczyć że dopiero zaczynam, i przepraszam za "głupie" posty.
package notatnik;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import static javax.swing.KeyStroke.getKeyStroke;
public class ActionFrame extends JFrame implements NewInterface {
private JTextArea t1;
private MenuBar m1;
private JMenu m2;
private JMenuItem zapisz, otworz, kopiuj, wklej;
private JButton button1, button2;
private JComboBox combo1;
private JPopupMenu jpm;
private File plik;
private String copy="";
public ActionFrame(){
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int x = (d.width) / 2;
int y = (d.height) / 2;
setSize(x, y);
setLayout(null);
/// Ustawianie okna i tytułu
m1 = new MenuBar();
setMenuBar(m1);
m2= new JMenu("Plik");
m2.setBounds(10, 10, 25, 50);
add(m2);
zapisz = new JMenuItem("zapisz");
add(zapisz);
otworz = new JMenuItem("otworz");
add(otworz);
m2.add(zapisz);
m2.add(otworz);
/// Menu i jego elementy
t1 = new JTextArea("Tu wpisuj tekst");
t1.setForeground(Color.green);
t1.setBackground(Color.WHITE);
t1.setBounds(50, 50, 500, 600);
JScrollPane jsp = new JScrollPane(t1);
jsp.setBounds(50, 50, 500, 600);
add(jsp);
/// Miejsce notatnika illPane
button1 = new JButton("zapis");
button1.setBackground(Color.red);
button1.setBounds(550, 350, 150, 100);
add(button1);
button1.addActionListener(this);
button2 = new JButton("otworz");
button2.setBackground(Color.red);
button2.setBounds(550, 250, 150, 100);
add(button2);
button2.addActionListener(this);
/// Przycisk Zapisz i Otworz
combo1 = new JComboBox();
combo1.setBounds(550, 150, 150, 100);
combo1.addItem("zielony");
combo1.addItem("czarny");
combo1.addItem("czerwony");
combo1.addItem("szary");
combo1.addItem("biały");
add(combo1);
combo1.addActionListener(this);
/// JComboBox do zmiany koloru
jpm = new JPopupMenu();
kopiuj = new JMenuItem("kopiuj");
kopiuj.setAccelerator(KeyStroke.getKeyStroke("ctrl + c"));
wklej = new JMenuItem("wklej");
wklej.setAccelerator(KeyStroke.getKeyStroke("ctrl + v"));
kopiuj.addActionListener(this);
wklej.addActionListener(this);
t1.setComponentPopupMenu(jpm);
jpm.add(kopiuj);
jpm.add(wklej);
/// JPopupMenu
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
Object c = e.getSource();
if(c==combo1){
String color;
color = combo1.getSelectedItem().toString();
if(color.equals("zielony")){
t1.setBackground(Color.green);
}
if(color.equals("czarny")){
t1.setBackground(Color.BLACK);
t1.setFont(new Font("SansSerif", Font.BOLD ,15));
}
if(color.equals("czerwony")){
t1.setBackground(Color.RED);
}
if(color.equals("szary")){
t1.setBackground(Color.GRAY);
}
if(color.equals("biały")){
t1.setBackground(Color.GREEN);
}
}
else if(c==kopiuj){
t1.copy();
}
else if(c==wklej){
t1.paste();
}
else if(c==zapisz || c==button1){
JFileChooser jc = new JFileChooser();
PrintWriter pw =null;
if(jc.showSaveDialog(null)==JFileChooser.APPROVE_OPTION){
File plik = jc.getSelectedFile();
try {
pw = new PrintWriter(plik);
}
catch (FileNotFoundException ex) {
Logger.getLogger(ActionFrame.class.getName()).log(Level.SEVERE, null, ex);
}
Scanner in = new Scanner(t1.getText());
while(in.hasNext()){
pw.println(in.nextLine()+"\n");
}
}
pw.close();
}
else if(c==otworz || c==button2){
JFileChooser jfc = new JFileChooser();
if(jfc.showOpenDialog(null)==JFileChooser.APPROVE_OPTION){
File plik = jfc.getSelectedFile();
Scanner scaner = null;
try {
scaner = new Scanner(plik);
t1.setText("");
} catch (FileNotFoundException ex) {
Logger.getLogger(ActionFrame.class.getName()).log(Level.SEVERE, null, ex);
}
while(scaner.hasNext()){
t1.append(scaner.nextLine()+"\n");
}
}
}
}
}
package notatnik;
import java.awt.EventQueue;
import javax.swing.JFrame;
public class Notatnik {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new ActionFrame();
}
});
}
}
PS. A i program nie jest na żadne zaliczenie żeby ktoś nie myślał że go wyzyskuję, mam 17 lat.