Witam. W programie nad którym właśnie pracuję potrzebuję przypisać do przycisku możliwość wklejania tekstu ze schowka. Mógłby ktoś podrzucić kilka linijek kodu?
Edit 1: Jeszcze mam taki problem, że gdy włączam go (jest to program w swingu) to JTextArea, JButtony i JMenuBar nie zawsze się wczytują. W przypadku JButtonów pojawiają się po przejechniu po nich myszką lub pojawiają się z standardowym stylem, a dopiero po przejechaniu po nich styl zmienia się na ten ustawiony.
- Rejestracja:około 7 lat
- Ostatnio:prawie 6 lat
- Postów:139
- Rejestracja:ponad 6 lat
- Ostatnio:6 dni
- Postów:3561
Tu masz dyskutowane podstrawowe wzroce clipboard
https://stackoverflow.com/questions/6710350/copying-text-to-the-clipboard-using-java
tu masz mój kod listenera na JButtonie, kopiuje zawartość widgeta errorTextArea
clipboardButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
clipboardButtonActionPerformed(evt);
}
});
....
private void clipboardButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clipboardButtonActionPerformed
Clipboard clipboard =
Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable transferable = new StringSelection(errorTextArea.getText());
clipboard.setContents(transferable, null);
}
Co do drugiego pytania, pachnie jakbyś nie dawał im czasu na odmalowanie się, albo za dużo robił w wątku GUI. Jakim fragmentem kodu powołujesz JFrame, czy co tam masz za rodzica tych nie malujących się
- Rejestracja:około 7 lat
- Ostatnio:prawie 6 lat
- Postów:139
Fragment kodu z całym GUI.
package Calculator.ViewAndDisplay;
import Calculator.Logic.Calculator;
import Calculator.Logic.Converter;
import Calculator.Logic.Filter;
import static Calculator.Logic.Filter.*;
import static Calculator.ViewAndDisplay.Display.deleteScreenContent;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.datatransfer.*;
import java.awt.Toolkit;
public class Window extends JFrame
{
protected static JTextArea screen;
private static JMenuBar menuBar;
private static JMenu menuInfo, menuTools;
private static JMenuItem itemInfo, itemToolsCopy, itemToolsPaste, exit;
private static String t0ext = "C", t6ext = "", t11ext = "", t17ext = "", t23ext = "",
t1ext = "B", t7ext = "", t12ext = "", t18ext = "", t24ext = "",
t2ext = "7", t8ext = "8", t13ext = "9", t19ext = "/", t25ext = "%",
t3ext = "4", t9ext = "5", t14ext = "6", t20ext = "*", t26ext = "()",
t4ext = "1", t10ext = "2", t15ext = "3", t21ext = "-", t27ext = "=",
t5ext = "0", t16ext = ".", t22ext = "+";
private static JButton b0utton = new JButton(t0ext), b10utton = new JButton(t10ext), b19utton = new JButton(t19ext),
b1utton = new JButton(t1ext), b11utton = new JButton(t11ext), b20utton = new JButton(t20ext),
b2utton = new JButton(t2ext), b12utton = new JButton(t12ext), b21utton = new JButton(t21ext),
b3utton = new JButton(t3ext), b13utton = new JButton(t13ext), b22utton = new JButton(t22ext),
b4utton = new JButton(t4ext), b14utton = new JButton(t14ext), b23utton = new JButton(t23ext),
b5utton = new JButton(t5ext), b15utton = new JButton(t15ext), b24utton = new JButton(t24ext),
b6utton = new JButton(t6ext), b16utton = new JButton(t16ext), b25utton = new JButton(t25ext),
b7utton = new JButton(t7ext), b17utton = new JButton(t17ext), b26utton = new JButton(t26ext),
b8utton = new JButton(t8ext), b18utton = new JButton(t18ext), b27utton = new JButton(t27ext),
b9utton = new JButton(t9ext);
public Window()
{
setSize(255, 307);
setResizable(false);
setTitle("Calculator");
setLayout(null);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
menuBar = new JMenuBar();
setJMenuBar(menuBar);
//info
menuInfo = new JMenu("Info");
itemInfo = new JMenuItem("informations");
itemInfo.addActionListener(e ->
{
new InfoWindow();
});
menuBar.add(menuInfo);
menuInfo.add(itemInfo);
//tools
menuTools = new JMenu("Tools");
itemToolsCopy = new JMenuItem("copy (ctrl + c");
itemToolsCopy.addActionListener(e ->
{
String myString = screen.getText();
StringSelection stringSelection = new StringSelection(myString);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, null);
});
itemToolsPaste = new JMenuItem("paste (ctrl + v)");
itemToolsPaste.addActionListener(e ->
{
});
//exit.setAccelerator(KeyStroke.getKeyStroke("ctrl x"));
exit = new JMenuItem("exit (ctrl + x)");
exit.addActionListener(e ->
{
dispose();
});
menuBar.add(menuTools);
menuTools.add(itemToolsCopy);
menuTools.add(itemToolsPaste);
menuTools.addSeparator();
menuTools.add(exit);
screen = new JTextArea();
screen.setBounds(10, 10, 224, 50);
screen.setEditable(false);
screen.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
screen.setFont(new Font("Courier", Font.ITALIC, 28));
screen.setLayout(null);
screen.addKeyListener(new KeyListener()
{
@Override
public void keyTyped(KeyEvent e)
{
Filter.charsFromKeyL(e);
}
@Override
public void keyPressed(KeyEvent e)
{
}
@Override
public void keyReleased(KeyEvent e)
{
}
});
add(screen);
b0utton.setBounds(10, 65, 42, 25);
b0utton.addActionListener(e ->
{
deleteScreenContent();
Filter.resetStatementSbAndResult();
Converter.resetResult();
Calculator.resetResult();
});
add(b0utton);
b1utton.setBounds(10, 95, 42, 25);
b1utton.addActionListener(e ->
{
charsFromActionL(t1ext);
});
add(b1utton);
b2utton.setBounds(10, 125, 42, 25);
b2utton.addActionListener(e ->
{
charsFromActionL(t2ext);
});
add(b2utton);
b3utton.setBounds(10, 155, 42, 25);
b3utton.addActionListener(e ->
{
charsFromActionL(t3ext);
});
add(b3utton);
b4utton.setBounds(10, 185, 42, 25);
b4utton.addActionListener(e ->
{
charsFromActionL(t4ext);
});
add(b4utton);
b5utton.setBounds(10, 215, 87, 25);
b5utton.addActionListener(e ->
{
charsFromActionL(t5ext);
});
add(b5utton);
//druga kolumna
b6utton.setBounds(55, 65, 42, 25);
b6utton.addActionListener(e ->
{
charsFromActionL(t6ext);
});
add(b6utton);
b7utton.setBounds(55, 95, 42, 25);
b7utton.addActionListener(e ->
{
charsFromActionL(t7ext);
});
add(b7utton);
b8utton.setBounds(55, 125, 42, 25);
b8utton.addActionListener(e ->
{
charsFromActionL(t8ext);
});
add(b8utton);
b9utton.setBounds(55, 155, 42, 25);
b9utton.addActionListener(e ->
{
charsFromActionL(t9ext);
});
add(b9utton);
b10utton.setBounds(55, 185, 42, 25);
b10utton.addActionListener(e ->
{
charsFromActionL(t10ext);
});
add(b10utton);
//trzecia kolumna
b11utton.setBounds(100, 65, 42, 25);
b11utton.addActionListener(e ->
{
charsFromActionL(t11ext);
});
add(b11utton);
b12utton.setBounds(100, 95, 42, 25);
b12utton.addActionListener(e ->
{
charsFromActionL(t12ext);
});
add(b12utton);
b13utton.setBounds(100, 125, 42, 25);
b13utton.addActionListener(e ->
{
charsFromActionL(t13ext);
});
add(b13utton);
b14utton.setBounds(100, 155, 42, 25);
b14utton.addActionListener(e ->
{
charsFromActionL(t14ext);
});
add(b14utton);
b15utton.setBounds(100, 185, 42, 25);
b15utton.addActionListener(e ->
{
charsFromActionL(t15ext);
});
add(b15utton);
b16utton.setBounds(100, 215, 42, 25);
b16utton.addActionListener(e ->
{
charsFromActionL(t16ext);
});
add(b16utton);
//czwarta kolumna
b17utton.setBounds(145, 65, 42, 25);
b17utton.addActionListener(e ->
{
charsFromActionL(t17ext);
});
add(b17utton);
b18utton.setBounds(145, 95, 42, 25);
b18utton.addActionListener(e ->
{
charsFromActionL(t18ext);
});
add(b18utton);
b19utton.setBounds(145, 125, 42, 25);
b19utton.addActionListener(e ->
{
charsFromActionL(t19ext);
});
add(b19utton);
b20utton.setBounds(145, 155, 42, 25);
b20utton.addActionListener(e ->
{
charsFromActionL(t20ext);
});
add(b20utton);
b21utton.setBounds(145, 185, 42, 25);
b21utton.addActionListener(e ->
{
charsFromActionL(t21ext);
});
add(b21utton);
b22utton.setBounds(145, 215, 42, 25);
b22utton.addActionListener(e ->
{
charsFromActionL(t22ext);
});
add(b22utton);
//piąta kolumna
b23utton.setBounds(190, 65, 42, 25);
b23utton.addActionListener(e ->
{
charsFromActionL(t23ext);
});
add(b23utton);
b24utton.setBounds(190, 95, 42, 25);
b24utton.addActionListener(e ->
{
charsFromActionL(t24ext);
});
add(b24utton);
b25utton.setBounds(190, 125, 42, 25);
b25utton.addActionListener(e ->
{
charsFromActionL(t25ext);
});
add(b25utton);
b26utton.setBounds(190, 155, 42, 25);
b26utton.addActionListener(e ->
{
charsFromActionL(t26ext);
});
add(b26utton);
b27utton.setBounds(190, 185, 42, 55);
b27utton.addActionListener(e ->
{
eqalTo();
});
add(b27utton);
}
}
Co do tego schowka, chodzi mi o schowek systemowy (o wklejanie ze schowka systemowego). Z tego co widziałem na stackoverflow to był tam kod który kopiuje i jakaś aplikacyja z jakimś własnym schowkiem.
- Rejestracja:ponad 6 lat
- Ostatnio:6 dni
- Postów:3561
Nie pokazujesz, jak Main tworzy pierwszą frame Window.
natomiast ja wszystkie okna Swingowe (SWT też) używam w oparciu o layouty, ręcznych pixeli niemal nigdy nie podałem.
Ostatnim wierszem konstruktora jest pack(); które daje czas i wymusza zarazem "niech rekurencyjnie wszystkie widgety się dogadają jak duże zamierzają być"
Wiem, że brak tego pack(); powodował późniejsze korygowanie okien (może podobne do efektu o jakim piszesz???). Nie uprawiam po zmianie kompa od wielu miesiący nic w Swingu, nie pomogę praktycznie.
- Rejestracja:około 7 lat
- Ostatnio:prawie 6 lat
- Postów:139
Tak wygląda metoda Main:
public static void main(String[] args)
{
Window window = new Window();
Filter filter = new Filter();
try
{
for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels())
{
if ("Nimbus".equals(info.getName()))
{
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
}
catch (Exception e)
{
System.out.println("LookAmdFeel Error");
}
}
Mógłbyś podać jakieś klasy, słowa kluczowe itp. odnośnie tych layoutów? Tak żebym miał jakiś punkt wyjścia.
- Rejestracja:około 7 lat
- Ostatnio:prawie 6 lat
- Postów:139
Dobra, wszytko działa. To:
try
{
for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels())
{
if ("Nimbus".equals(info.getName()))
{
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
}
catch (Exception e)
{
System.out.println("LookAmdFeel Error");
}
przeniosłem do konstruktora, a na końcu konstruktora dałem:
pack();
setSize(255, 307);
setVisible(true);
i śmiga. Zawsze ładuje się od razu dzięki za pomoc.
- Rejestracja:około 7 lat
- Ostatnio:prawie 6 lat
- Postów:139
A je jeszcze jedno, czy da się zrobić tak aby JTextArea było nieklikalne? Tak aby nie pojawiała się niebieska ramka po kliknięciu na nie. Mam setEditable(false);