Napisałem jeszcze raz tą cześć która nie działała i teraz działa. Mam za to jeszcze pytanie: mam JTextArea i chciałbym żeby wielkość okienka zwiększała swoją szerokość podczas dopisywania do niej do np 350 a następnie wysokość już bez ograniczeń. Próbowałem użyć MinSize i MaxSize ale nie uzyskałem oczekiwanych rezultatów ponieważ okienko robi się szerokie tylko na długość najdłuższego słowa a następnie zwiększa wysokość. Wie ktoś może jak to ustawić ?
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Czat3 {
private JFrame frmAutoczat;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Czat3 window = new Czat3();
window.frmAutoczat.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Czat3() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmAutoczat = new JFrame();
frmAutoczat.setTitle("AutoCzat");
frmAutoczat.setBounds(100, 100, 454, 519);
frmAutoczat.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmAutoczat.getContentPane().setLayout(null);
setNimbus();
JTextArea wprowadzTekst = new JTextArea();
wprowadzTekst.setLineWrap(true);
wprowadzTekst.setWrapStyleWord(true);
JScrollPane scrollWprowadzTekst = new JScrollPane(wprowadzTekst);
scrollWprowadzTekst.setBounds(10, 321, 418, 102);
frmAutoczat.getContentPane().add(scrollWprowadzTekst);
JButton btnWylij = new JButton("Wy\u015Blij >>");
btnWylij.setBounds(339, 430, 89, 23);
frmAutoczat.getContentPane().add(btnWylij);
JButton btnWyczy = new JButton("Wyczy\u015B\u0107");
btnWyczy.setBounds(10, 430, 89, 23);
frmAutoczat.getContentPane().add(btnWyczy);
JScrollPane scrollWyswietl = new JScrollPane();
scrollWyswietl.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollWyswietl.setBounds(10, 11, 418, 298);
scrollWyswietl.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
frmAutoczat.getContentPane().add(scrollWyswietl);
JPanel wyswietl = new JPanel();
wyswietl.setMaximumSize(new Dimension(100, 32767));
scrollWyswietl.setViewportView(wyswietl);
wyswietl.setLayout(new BoxLayout(wyswietl, BoxLayout.Y_AXIS));
JButton btnNewButton = new JButton("New button");
btnNewButton.setBounds(99, 434, 89, 23);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
scrollDown(scrollWyswietl);
}
});
frmAutoczat.getContentPane().add(btnNewButton);
JButton btnNewButton_1 = new JButton("New button");
btnNewButton_1.setBounds(277, 430, 52, 23);
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JPanel panelTmp = new JPanel();
wyswietl.add(panelTmp);
panelTmp.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));
JTextArea atxt = new JTextArea();
atxt.setFont(new Font("Monospaced", Font.PLAIN, 14));
atxt.setAlignmentX(Component.RIGHT_ALIGNMENT);
atxt.setWrapStyleWord(true);
atxt.setLineWrap(true);
// atxt.setBounds(0, 0, 350, 20);
atxt.setMaximumSize(new Dimension(350,9999999));
atxt.setMinimumSize(new Dimension(100,5));
atxt.setText("aaaaaaaaaaaa iughuyg ug iugiu iu huih iu tyf7uyguygiugiughiu yugiugiughiugigu iuguigiugiugiughi ");
panelTmp.add(atxt);
scrollWyswietl.validate();
}
});
frmAutoczat.getContentPane().add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("New button");
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JPanel panelTmp = new JPanel();
wyswietl.add(panelTmp);
panelTmp.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
JTextArea atxt = new JTextArea();
atxt.setFont(new Font("Monospaced", Font.PLAIN, 14));
atxt.setAlignmentX(Component.RIGHT_ALIGNMENT);
atxt.setWrapStyleWord(true);
atxt.setLineWrap(true);
atxt.setBounds(0, 0, 350, 20);
atxt.setText("aaaaaaaaaaaa ffffffffffffffff ddddddddd i gui iu iu uigi uguigiygiy ugiugiuguioguio giugiugui ougou guo guoig iug iug iug iug iu ");
panelTmp.add(atxt);
scrollWyswietl.validate();
}
});
btnNewButton_2.setBounds(204, 434, 67, 23);
frmAutoczat.getContentPane().add(btnNewButton_2);
JMenuBar menuBar = new JMenuBar();
frmAutoczat.setJMenuBar(menuBar);
JMenu mnPlik = new JMenu("Plik");
menuBar.add(mnPlik);
JMenuItem mntmNewMenuItem_2 = new JMenuItem("Wczytaj plik bazowy");
mnPlik.add(mntmNewMenuItem_2);
JMenuItem mntmNewMenuItem_1 = new JMenuItem("Zamknij");
mnPlik.add(mntmNewMenuItem_1);
JMenu mnStatystyka = new JMenu("Statystyka");
menuBar.add(mnStatystyka);
JMenuItem mntmNewMenuItem = new JMenuItem("Wy\u015Bwietl statystyki");
mnStatystyka.add(mntmNewMenuItem);
JMenu mnPomoc = new JMenu("Pomoc");
menuBar.add(mnPomoc);
JMenuItem mntmOProgramie = new JMenuItem("O programie");
mnPomoc.add(mntmOProgramie);
}
private void scrollDown(JScrollPane s){
JScrollBar a = s.getVerticalScrollBar();
a.setValue(a.getMaximum());
}
private void setNimbus() {
try {
UIManager
.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (ClassNotFoundException | InstantiationException
| IllegalAccessException | UnsupportedLookAndFeelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SwingUtilities.updateComponentTreeUI(frmAutoczat);
}
}